我有一个模型:
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(user = current_user)?
在Essai_Temperature中,字段user
不起作用
如何在材质中访问字段用户?
答案 0 :(得分:0)
你以完全错误的方式思考这个问题。来自Essai的外键是Material,而不是User,因此你应该将其称为material
(并删除related_name
,这是错误和令人困惑的。)
现在你可以关注这个关系:
qs.filter(material__user=current_user)