[英]Join two models together using a common column
I have two tables 我有两张桌子
Table 1 表格1
CREATE TABLE `profiles` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`first_name` varchar(45) DEFAULT NULL,
`surname` varchar(45) DEFAULT NULL,
`email` varchar(45) DEFAULT NULL,
`unique_id` varchar(100) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id_UNIQUE` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;
Table 2 表2
CREATE TABLE `bible_photo` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`file` varchar(100) NOT NULL,
`file_name` varchar(45) DEFAULT NULL,
`unique_id` varchar(100) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;
And two corresponding models 和两个对应的模型
Models 楷模
class TProfiles(models.Model):
id = models.IntegerField(primary_key=True) # AutoField?
first_name = models.CharField(max_length=45, blank=True)
surname = models.CharField(max_length=45, blank=True)
email = models.CharField(max_length=45, blank=True)
unique_id = models.CharField(max_length=100, blank=True)
class Meta:
managed = False
db_table = 'profiles'
class Photo( models.Model ):
file = models.FileField( upload_to = settings.MEDIA_ROOT )
file_name = models.CharField(max_length=45, blank=True)
unique_id = models.CharField(max_length=100, blank=True)
def extension(self):
name, extension = os.path.splitext(self.file.name)
return extension
I want to access all profiles and their corresponding profile picture, based on the unique ID they both share. 我想根据它们共享的唯一ID访问所有个人资料及其对应的个人资料图片。 I have the following in my views.py file.
我的views.py文件中包含以下内容。 However, although I can access them both individually, I cannot combine them to pass to the template.
但是,尽管我可以分别访问它们,但不能将它们组合以传递给模板。
views.py views.py
def register(request):
photos = Photo.objects.all
profiles = TProfiles.objects.all
return render_to_response("register.html", {
"form": form, "photos": photos,
}, RequestContext(request))
Update 更新
I tried adding profile = models.ForeignKey(TProfiles)
to my Photo model, but I keep getting complaints about missing columns relating to the Profiles table. 我尝试将
profile = models.ForeignKey(TProfiles)
到我的Photo模型中,但是我一直抱怨缺少与Profiles表相关的列。 Surely I don't have to duplicate them in to Photos table? 当然,我不必将它们复制到“照片”表中吗?
OperationalError at /register/
(1054, "Unknown column 'bible_photo.profile_id' in 'field list'")
And this is the line of code in my template that it is moaning about. 这就是我抱怨的模板中的代码行。 I am trying to see if I can access the profile from the photo.
我正在尝试查看是否可以通过照片访问个人资料。
{% for p in photos %}
<h1>{{ p.profile }}</h1>
{% endfor %}
change your models.py
like this: 像这样更改您的
models.py
:
class TProfiles(models.Model):
first_name = models.CharField(max_length=45, blank=True)
surname = models.CharField(max_length=45, blank=True)
email = models.CharField(max_length=45, blank=True)
unique_id = models.CharField(max_length=100, blank=True)
class Meta:
managed = False
db_table = 'profiles'
class Photo( models.Model ):
file = models.FileField( upload_to = settings.MEDIA_ROOT )
file_name = models.CharField(max_length=45, blank=True)
unique_id = models.CharField(max_length=100, blank=True)
event_location_time = models.ForeignKey(TProfiles)
def extension(self):
name, extension = os.path.splitext(self.file.name)
return extension
And change your view
like this: 并按以下方式更改
view
:
def register(request):
profiles = TProfiles.objects.all()
return render_to_response("register.html", {
"form": form, "profiles": profiles,
}, RequestContext(request))
And in your template
you can use : 在
template
您可以使用:
{% for profile in profiles %}
#access to profile object
{% for photo in profile.photo_set.all %}
#access to each profile photo object
{% endfor %}
{% endfor %}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.