我正在尝试为用户提供一种“观看”某些项目的方法(即,将项目添加到其他用户包含其他项目的列表中):
class WatchList(models.Model):
user = models.ForeignKey(User)
class Thing(models.Model):
watchlist = models.ForeignKey(WatchList, null=True, blank=True)
如何Thing
向用户添加WatchList
?
>>> from myapp.models import Thing
>>> z = get_object_or_404(Thing, pk=1)
>>> a = z.watchlist.add(user="SomeUser")
AttributeError: 'NoneType' object has no attribute 'add'
如何将商品添加到监视列表?和/或这是设置模型字段的适当方法吗?感谢您的任何想法!
z.watchlist
是参考本身,而不是客户关系经理。只需分配:
z.watchlist = WatchList.objects.get(user__name='SomeUser')
请注意,这假设WatchList
每个用户只有一个。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句