开发者

Trying and expand the contrib.auth.user model and add a "relationships" manage

开发者 https://www.devze.com 2023-02-03 03:20 出处:网络
I have the following model setup. from django.db import models from django.contrib.auth.models import User

I have the following model setup.

from django.db import models
from django.contrib.auth.models import User

class SomeManager(models.Manager):
    def friends(self):
        # return friends bla bla bla


class Relationship(models.Model):
    """(Relationship description)"""
    from_user = models.ForeignKey(User, related_name='from_user')
    to_user = models.ForeignKey(User, related_name='to_user')
    has_requested_friendship = models.BooleanField(default=True)
    开发者_如何转开发is_friend = models.BooleanField(default=False)

    objects = SomeManager()

relationships = models.ManyToManyField(User, through=Relationship, symmetrical=False)
relationships.contribute_to_class(User, 'relationships')

Here i take the User object and use contribute_to_class to add 'relationships' to the User object. The relationship show up, but if call User.relationships.friends it should run the friends() method, but its failing. Any ideas how i would do this?

Thanks


I would do this instead.

f= Relationships.objects.filter( from_user=some_user, is_friend=True )

That saves trying to mess around with User.


Maybe you're missing the ".objects" in the call:

User.relationships.objects.friends()


You need to tell Django to use your manager for related lookups - by default, it uses a standard one for those. You need to set use_for_related_fields on the Manager - see the documentation on this.

0

精彩评论

暂无评论...
验证码 换一张
取 消