'RelatedManager' object has no attribute

后端 未结 2 874
有刺的猬
有刺的猬 2021-01-31 08:13

I have a model defined like this:

class UserDetail(models.Model):
    user = models.ForeignKey(User, db_index=True, unique=True, related_name=\'details\')
    fa         


        
2条回答
  •  醉梦人生
    2021-01-31 08:57

    When you access user.details, it accesses the backreference of the UserDetail.user foreign key. The foreign Key itself doesn't specify that a User can only have one UserDetail, so django gives you a RelatedManager, which you can filter and query just like a regular Manager. So you do the same things to it that you do to your .objects managers. You can ask for user.details.all(), user.details.filter(), user.details.get(), and so on, which will either give you a queryset, an object, or an exception,depending on the method and the results.

提交回复
热议问题