的外鍵我有一個模型:Django的:如何聲明另一個外鍵
class Material(models.Model):
name = models.CharField(_('name'), max_length=50)
description = models.TextField(_('description'), blank=True)
user = models.ForeignKey(User, default=None, blank= True, null = True)
class Essai_Temperature(models.Model):
name = models.ForeignKey(Material, verbose_name=_('name'))
nature_unit = models.ForeignKey(Property, verbose_name=_('category'))
user= models.ForeignKey(Material, related_name="user_set", default='0')
管理:
class Essai_TemperatureAdmin(admin.ModelAdmin):
def queryset(self, request):
qs = super(Essai_TemperatureAdmin, self).queryset(request)
current_user = request.user
if current_user.is_superuser:
return qs
else:
return qs.filter(user=current_user)
如何返回qs.filter(用戶= CURRENT_USER)?
在Essai_Temperature領域user
不工作
我怎麼能有機會獲得在材料領域的用戶?
我認爲你可以使用像'qs.filter(user__user = CURRENT_USER)' – trnsnt
相同的結果,如果我把qs.none() – user3172700
看到文檔,@trnsnt是正確的https://docs.djangoproject.com/en/dev/topics/db/queries/#lookups-that-span-relationships – Anentropic