简体   繁体   中英

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). This extra field is called 'favorites' and the posts favorited by a user should go there. 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.

# 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?

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. 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. I did this by writing a function in my views.py:

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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