Thursday, January 21, 2016

Redefine RelatedManager

Hello.

Is there any way to redefine a relatedmanager for one field with foreignkey? 

For example:

class Contract(models.Model):
   name = models.CharField(max_length=255, db_index=True)
   owner = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='contracts')

class AppUser(AbstractUser):
    account_manager = models.ForeignKey('self', null=True, blank=True, related_name='clients')

    @property
    def is_manager(self):
        return self.groups.filter(name='managers').exists()

    @property
    def is_client(self):
        return self.groups.filter(name='clients').exists()
________________________________________________________

manager = AppUser.objects.get(id=1)
manager.contracts.all()

client = AppUser.objects.get(id=2)
client.contracts.all()

When I call manager.contracts.all(), I s that associated witwant to get contracth this manager and his clients contracts. And when I call client.contracts.all(), I want to get only client's contracts. 




--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/e6159779-b3d6-4a4a-8751-6b99ff9ea4f0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment