I need to fetch columns from multiple tables, how do I achieve this?
我需要从多个表中获取列,我该如何实现?
UserLog
+------------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+----------+------+-----+---------+----------------+
| log_id | int(11) | NO | PRI | NULL | auto_increment |
| time | datetime | NO | | NULL | |
| ip_address | char(15) | YES | | NULL | |
| event_id | int(11) | NO | MUL | NULL | |
| user_id | int(11) | NO | MUL | NULL | |
+------------+----------+------+-----+---------+----------------+
Events
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| event_id | int(11) | NO | PRI | NULL | auto_increment |
| description | varchar(200) | NO | | NULL | |
+-------------+--------------+------+-----+---------+----------------+
This is my models.py
这是我的models.py
class Events(models.Model):
event_id = models.AutoField(primary_key=True,verbose_name="Event id")
description = models.CharField(max_length=200,verbose_name="Description")
def __unicode__(self):
return u"%d : %s" % (self.event_id, self.description)
class UserLog(models.Model):
log_id = models.AutoField(primary_key = True)
user = models.ForeignKey(User)
event = models.ForeignKey(Events,verbose_name = "event")
time = models.DateTimeField(default=datetime.now,blank=True)
ip_address = models.IPAddressField(null=True)
def __unicode__(self):
return u"%s (%s): %s" % (self.log_id, self.user, self.event)
Spcifically I want to do SELECT log_id,time,user_id,event_id,description from UserLog,Events where UserLog.event_id = Events.event_id
特别地,我想从UserLog执行SELECT log_id,time,user_id,event_id,描述,UserLog.event_id = Events.event_id
I tried UserLog.objects.filter(event_id__event_id).values('log_id','time','user_id','event_id','description')
. But it says no such fields.
我尝试了UserLog.objects.filter(event_id__event_id).values('log_id','time','user_id','event_id','description')。但它没有说这样的领域。
Is there something I need to change in my models class?
我的模型课中有什么需要改变的吗?
2 个解决方案
#1
3
Try this:
UserLog.objects.filter(event_id=1).values('log_id',
'time',
'user_id',
'event_id',
'event__description')
Alternately, you could use select_related:
或者,您可以使用select_related:
UserLog.objects.filter(event__id=1).select_related('event')
then you'll get UserLog
objects, with the event pre-fetched, so you can access the event without triggering another query.
然后你将获得UserLog对象,并预先获取事件,这样你就可以访问事件而不会触发另一个查询。
(Also, standard naming convention would be to call the model Event
, not Events
)
(另外,标准命名约定是调用模型事件,而不是事件)
#2
0
use select_related() like this:
像这样使用select_related():
UserLog.objects.filter(event_id__event_id=1).select_related('event').values('log_id','time','user_id','event_id', 'event__description')
#1
3
Try this:
UserLog.objects.filter(event_id=1).values('log_id',
'time',
'user_id',
'event_id',
'event__description')
Alternately, you could use select_related:
或者,您可以使用select_related:
UserLog.objects.filter(event__id=1).select_related('event')
then you'll get UserLog
objects, with the event pre-fetched, so you can access the event without triggering another query.
然后你将获得UserLog对象,并预先获取事件,这样你就可以访问事件而不会触发另一个查询。
(Also, standard naming convention would be to call the model Event
, not Events
)
(另外,标准命名约定是调用模型事件,而不是事件)
#2
0
use select_related() like this:
像这样使用select_related():
UserLog.objects.filter(event_id__event_id=1).select_related('event').values('log_id','time','user_id','event_id', 'event__description')