1👍
✅
class RegisteredUser(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
email = models.EmailField(unique=True)
password = models.CharField(max_length=10)
connected_users = models.ManyToManyField('self', blank=True, symmetrical=False)
def __unicode__(self):
return self.email
the api now looks like this:
ru = RegisteredUser.objects.get(id=1)
another_user = RegisteredUser.objects.get(email='name@example.com')
ru.connected_users.add(another_user)
uc = ru.connected_users.all()
for user in uc:
print user.first_name, user.last_name, user.email
0👍
The Best way to Implement friendship is to use ManyToManyField in Django:
You can read more about it here: https://docs.djangoproject.com/en/1.6/ref/models/fields/#ref-manytomany
Also, to resolve you confusion about query, with ‘uc’ variable you can just do:
uc.email
for email id
and
uc.user_connection.first_name
for firstname .
You can read More about Django Queries and objects here : https://docs.djangoproject.com/en/1.6/topics/db/queries/
- [Answer]-Django non persistent model field
- [Answer]-How to create a ListView with an argument | Django 1.6?
- [Answer]-Understanding Django (from source code): attributes of field via models.CharField and forms.CharField
- [Answer]-Django Factory Boy letter generator
Source:stackexchange.com