Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

related name in django

Let's say you have a model named Book and a model named Category. Each book has one and only one category, denoted by a foreign key. Thus, you'll have the following models:

class Category(models.Model):
  name = models.CharField(max_length=128)

class Book(models.Model):
  name = models.CharField(max_length=128)
  category = models.ForeignKey('Category')
Now, when you have a Book instance you can refer to its category using 
the corresponding field. Furthermore, if you have a category instance,
by default, django adds an attribute to it named book_set which returns
a queryset with all the books that have this specific category.
So you can do something like:

category = Category.objects.get(pk=1)
print "Books in category {0}".format(category.name)
for book in category.book_set.all():
  print book.name
Now, book_set is an attribute that django constructed for us and gave it this
name by default. Using the related_name attribute of foreign key you can give
this attribute whatever name you want (for example if I had definited category
                                       as this
                                       category = models.ForeignKey('Category', related_name='book_collection') then instead of category.book_set.all() I'd use category.book_collection.all()).

In any case, you rarely need to change the related_name, if at all in usual case 
                                       (I don't recommend it because it's easy to remember the django default x_set).
                                       However there's a use case where it is required: When you have multiple
                                       foreign keys from a model to another. 
                                       In this case there would be a clash (since django would try to create two x_set attributes to the same model) and you need to help by naming 
                                       the x_set attributes yourself.

For example, if my Book model was like this (had a category and a subcategory):

class Book(models.Model):
  name = models.CharField(max_length=128)
  category = models.ForeignKey('Category')
  sub_category = models.ForeignKey('Category')
then the model would not validate unless you give one (or both) of the 
                                       ForeignKeys a related_name attribute so that the clash will be resolved. 
                                       For example you could do something like this:

class Book(models.Model):
 name = models.CharField(max_length=128)
  category = models.ForeignKey('Category', related_name='book_category_set')
  sub_category = models.ForeignKey('Category', related_name='book_sub_category_set')
Comment

PREVIOUS NEXT
Code Example
Python :: django phone number 
Python :: python how to initialize wikipediaapi 
Python :: python numpy find local minima 
Python :: newspaper pypi 
Python :: plot idl 
Python :: python creare decoratori 
Python :: Find the minimum item in this RDD 
Python :: if function error grepper 
Python :: assign multiple vabies in one line 
Python :: python loop over s3 objects] 
Python :: pandas filter rows by fuzzy values 
Python :: corona data with python flask get pdf 
Python :: count number of repeats in list python 
Python :: Programmatically determining programming languages based on file extensions in python 
Python :: simplest flask memcached 
Python :: cv2.puttext 
Python :: gtk entry focus python 
Python :: read stripped lines from a file python 
Python :: discord.File(fp=image_binary,filename=name) discord py 
Python :: what does 0 for in array mean python 
Python :: how to delete a cell in jupyter notebook 
Python :: printf("Enter the second number: ") 
Python :: pycaw , Python Audio Control Lib 
Python :: scattter_matrix pandas 
Python :: online convert http query to json python 
Python :: Python run module with and without "-m" option and import path setting 
Python :: how to plot a counter output 
Python :: drawmolecule rdkit 
Python :: Python Split list into chunks using itertools Method 
Python :: python non public method 
ADD CONTENT
Topic
Content
Source link
Name
4+4 =