简体   繁体   English

如何添加到默认 Django 用户 model 的 ManyToManyField 扩展?

[英]How can you add to a ManyToManyField extension of the default Django User model?

For my app, I want to add one extra ManyToManyField to the default User model (django.contrib.auth.models.User).对于我的应用程序,我想向默认用户 model (django.contrib.auth.models.User) 添加一个额外的 ManyToManyField。 This extra field is called 'favorites' and the posts favorited by a user should go there.这个额外的字段称为“收藏夹”,用户收藏的帖子应该在 go 那里。 This is what I have:这就是我所拥有的:

class Favorite(models.Model):
    user = models.OneToOneField(User, related_name='favorites', on_delete=models.CASCADE)
    favorites = models.ManyToManyField(Recipe, related_name='favorited_by')

This is what I get when trying to add to 'favorites' from the shell.这就是我尝试从 shell 添加到“收藏夹”时得到的结果。

# imported Recipe, Favorite, User(default)
>>> recipe1 = Recipe.objects.all()[0]
>>> me = User.objects.all()[0]
>>> me.favorites.add(recipe1)
django.contrib.auth.models.User.favorites.RelatedObjectDoesNotExist: User has no favorites.

# Just checking if the the User object, me, has a 'favorites' attribute
>>> 'favorites' in dir(me)
True

What is the correct way to add a Recipe object to this 'favorites' field?将配方 object 添加到此“收藏夹”字段的正确方法是什么?

For more reference, I did something similar how I handled Friendships between users, but it was a bit simpler since I wasn't extending the User model.为了获得更多参考,我在处理用户之间的友谊时做了类似的事情,但它更简单一些,因为我没有扩展用户 model。 The code for that is below and works fine:代码如下并且工作正常:

class Friend(models.Model):
    users = models.ManyToManyField(User)
    current_user = models.ForeignKey(User, related_name='owner', null=True, on_delete=models.CASCADE)

    @classmethod
    def make_friend(cls, current_user, new_friend):
        friend, created = cls.objects.get_or_create(
            current_user=current_user
        )
        friend.users.add(new_friend)

    @classmethod
    def lose_friend(cls, current_user, new_friend):
        friend, created = cls.objects.get_or_create(
            current_user=current_user
        )
        friend.users.remove(new_friend)

Resolved.解决。 My solution is below, but I'm not sure if this is good practice.我的解决方案如下,但我不确定这是否是好的做法。

django.contrib.auth.models.User.favorites.RelatedObjectDoesNotExist: User has no favorites.

The User model may have the 'favorites' field, but I needed to actually fill it with a 'Favorite' object.用户 model 可能有“收藏夹”字段,但我实际上需要用“收藏夹”object 填充它。 I did this by writing a function in my views.py:我通过在我的views.py中写了一个function来做到这一点:

def add_favorite(request, pk):
    # Check if the user has a favorites field. If not create one and add. If yes, just add
    user_favorites, created = Favorite.objects.get_or_create(
        user=request.user
        )
    recipe = get_object_or_404(Recipe, pk=pk)
    user_favorites.favorites.add(recipe)

This seems to work and I can access a user's favorites now, but I maybe this isn't good practice.这似乎可行,我现在可以访问用户的收藏夹,但我可能这不是一个好习惯。 With my method, new models that are created do not have a 'Favorite' object within it.使用我的方法,创建的新模型中没有“最喜欢的”object。 That will only get created when a user decides to add a favorite recipe and the above view will create one if it doesn't already exist.只有当用户决定添加一个最喜欢的食谱时才会创建它,如果上面的视图不存在,它将创建一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM