class Person(models.Model):
name = models.CharField(max_length=128)
def __unicode__(self):
return self.name
class Group(models.Model):
name = models.CharField(max_length=128)
members = models.ManyToManyField(Person,through='Membership')
def __unicode__(self):
return self.name
class Membership(models.Model):
person = models.ForeignKey(Person)
group = models.ForeignKey(Group)
date_joined = models.DateField()
invite_reason = models.CharField(max_length=64)
>>> ringo = Person.objects.create(name="Ringo Starr")
>>> paul = Person.objects.create(name="Paul McCartney")
>>> beatles = Group.objects.create(name="The Beatles")
>>> m1 = Membership(person=ringo,group=beatles,... date_joined=date(1962,8,16),... invite_reason= "Needed a new drummer.")
>>> m1.save()
>>> beatles.members.all()
[
我的模型看起来像这样:
class Trip(models.Model):
members = models.ManyToManyField(User,blank=True,null=True,through='TripReservation')
最佳答案
首先,您使用的是直通模型吗?你已经在那里,但你没有列出它.如果你不是,你不需要它.
原文链接:https://www.f2er.com/python/439105.html我会添加一个related_name,如下所示:
class Trip(models.Model):
members = models.ManyToManyField(User,related_name='user_trips')
然后你应该可以打电话:
user.user_trips.all()
我称之为’user_trips’而不是’trip’,因为如果它不是唯一的名称,它可能会导致冲突.
如果您使用的是直通模型,它看起来会更像这样:
#User is defined in django.auth
class Trip(models.Model):
members = models.ManyToManyField(User,related_name='user_trips',through='TripReservation')
class TripReservation(models.Model):
user = models.ForeignKey(User)
trip = models.ForeignKey(Trip)
registered = models.DateField()
通过这种方式了解TripReservation是指特定用户对Trip的预订,而不是整个行程,有关行程的信息应该是Trip模型本身的属性.因此,TripReservation.registered是指特定用户注册旅行的时间.
用户旅行查找将是相同的:
user.user_trips.all()