What is related name used for in Django

0 votes

What is the related_name argument useful for on ManyToManyField and ForeignKey fields? For example, given the following code, what is the effect of related_name='maps'?

class Map(db.Model):
    members = models.ManyToManyField(User, related_name='maps',
                                     verbose_name=_('members'))
Jun 26, 2020 in Python by kartik
• 37,520 points
10,801 views

1 answer to this question.

0 votes

Hello @kartik,

The related_name attribute specifies the name of the reverse relation from the User model back to your model.

If you don't specify a related_name, Django automatically creates one using the name of your model with the suffix _set, for instance User.map_set.all().

If you do specify, e.g. related_name=maps on the User model, User.map_set will still work, but the User.maps. syntax is obviously a bit cleaner and less clunky; so for example, if you had a user object current_user, you could use current_user.maps.all() to get all instances of your Map model that have a relation to current_user.

Hope it helps!!

Thank You!

answered Jun 26, 2020 by Niroj
• 82,880 points

Related Questions In Python

0 votes
1 answer

.join() method is used for what purpose in Python?

join() method is used to concatenate or ...READ MORE

answered May 14, 2019 in Python by Taj
• 1,080 points
1,090 views
0 votes
1 answer

assert keyword in python is used for what purpose?

The assert keyword is used while debugging ...READ MORE

answered May 24, 2019 in Python by Taj
• 1,080 points
3,972 views
0 votes
1 answer

What is the patterns package in python used for?

Pattern is a web mining tool in ...READ MORE

answered Jul 31, 2019 in Python by Mohammad
• 3,230 points
707 views
0 votes
1 answer

What is the flow control for “continue” in python?

This is the way "continue" statement works! You ...READ MORE

answered Jul 16, 2018 in Python by Priyaj
• 58,100 points
749 views
0 votes
1 answer

Error:Python3.4 can't install mysql-python

Hello @kartik, You can resolved this by the ...READ MORE

answered Jun 24, 2020 in Python by Niroj
• 82,880 points
2,106 views
0 votes
1 answer

Django Server Error: port is already in use

Hello @kartik, Just type: sudo fuser -k 8000/tcp. This ...READ MORE

answered Jun 26, 2020 in Python by Niroj
• 82,880 points
1,726 views
+1 vote
1 answer

How can I build multiple submit buttons django form?

Hiii @kartik, You can use self.data in the clean_email method to access ...READ MORE

answered Jun 26, 2020 in Python by Niroj
• 82,880 points
2,705 views
0 votes
1 answer

What is the equivalent of “none” in django templates?

Hello @kartik, None, False and True all are available ...READ MORE

answered Jul 3, 2020 in Python by Niroj
• 82,880 points
8,857 views
+1 vote
4 answers

In Python, what is difference between Array and List?

Lists and arrays are used in Python ...READ MORE

answered Mar 15, 2019 in Python by Taj
• 1,080 points

edited Mar 18, 2019 by Omkar 152,720 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP