我有两个带外键的型号。
class Ad(models.Model):
town = models.CharField(max_length=30)
owner = models.ForeignKey(SpaUser, on_delete=models.CASCADE) #My foreign key
contact = SpaUser.objects.filter(email= 'owner').values('firstname ') # I want to access SpaUser.firstname to display in django admin
class SpaUser(models.Model):
email = models.EmailField(verbose_name='email address', max_length=255, unique=True)
firstname = models.CharField(max_length=30, blank=True)
lastname = models.CharField(max_length=30, blank=True)
我在广告模型中使用Ad.contact来在Spauser模型中访问SpaUser.firstname,但我不能。 请帮助我
答案 0 :(得分:0)
您可以改为使contact
为属性(并且应该使用外键owner
而不是从SpaUser
的另一个查询集中进行过滤):
class Ad(models.Model):
@property
def contact(self):
return self.owner.firstname