1
votes

I am trying to build a friendship relation in my project by using django-friendship package. The problem is that I what to build the relationship between my User model and my own model called Organisation. But when I am trying to do this it turned out that django-friendship supports only relationship between two objects of the same type (for example of type User-User). I even tried to override the existing models in django-friendship in order to change the methods but without any success. At the moment I am on the stage that the friend request is sent from User to Organisation but I need to accept it and there is where I struggle.

friendship/models.py

class FriendshipRequest(models.Model):
    """ Model to represent friendship requests """
    from_user = models.ForeignKey(user_model, related_name='organisation_requests_sent')
    to_user = models.ForeignKey(organisation_model, related_name='organisation_requests_received')

    def accept(self):
        """ Accept this friendship request """
        relation1 = Friend.objects.create(
            from_user=self.from_user,
            to_user=self.to_user
        )

        relation2 = Friend.objects.create(
            from_user=self.to_user,
            to_user=self.from_user
        )

        friendship_request_accepted.send(
            sender=self,
            from_user=self.from_user,
            to_user=self.to_user
        )

class Friend(models.Model):
    """ Model to represent Friendships """
    to_user = models.ForeignKey(user_model, related_name='organisations')
    from_user = models.ForeignKey(organisation_model, related_name='_unused_organisation_relation')

The problem is that when it creates relationship1 and relationship2 it crashes because to_user and from_user are in a relationship with different models. I would be really glad if you help me with the problem or recommend another way (without using django-friendship package) of doing it in order to fit my requirements.

1
This sort of thing can be done quite easily without third party packages. Learning these packages takes more time than doing it on your own - e4c5
mm, do you have anything in mind or something to recommend me, tutorial or documentation, because I spend quite a lot of time looking through youtube and other resources to see if someone can help me but couldn't find anything :/ - Simeon Kostadinov

1 Answers

0
votes

https://github.com/Ry10p/django-Plugis/blob/master/courses/models.py

Line 52

Also the position of the Friend model must be before the FriendRequest model, since the FriendRequest model relies on Friend for a ForeignKey.

Example:

class Author():
    name = models.CharField(max_length=100)
    bio = models.TextField(max_length=100)


class Article():
    author = models.ForeignKey(Author)
    text = models.TextField(max_length=500)
    publish_date = models.DateTimeField(null=True)