2

I have learned about the database concepts in django tutorial book. I have some doubts about to fetch data from the table in sql database server. In django book they explained something like this to filter data as like as below

Entry.objects.filter(pub_date__year=2007).update(headline='Everything is the same')

Here Entry is the class name which is defined in models.py file like below

models.py

class Entry(models.Model):
   blog = models.ForeignKey(Blog)
   headline = models.CharField(max_length=255)
   body_text = models.TextField()
   pub_date = models.DateField()
   mod_date = models.DateField()
   authors = models.ManyToManyField(Author)
   n_comments = models.IntegerField()
   n_pingbacks = models.IntegerField()
   rating = models.IntegerField()

   def __str__(self):              # __unicode__ on Python 2
       return self.headline

In the above method there is nothing mention about the table to modified. Then which table it is going to be modified in the below query.

  Entry.objects.filter(pub_date__year=2007).update(headline='Everything is the same')

If i asked anything wrong please forgive me. Can anyone clear my doubts.

1
  • Think of an Entry object with attributes/fields (blog, headline, body_text, etc.) instead of a table; then, you're updating the headline of all entries whose pub_date is 2007 Commented Nov 17, 2014 at 9:11

2 Answers 2

3

From the docs on table names:

To save you time, Django automatically derives the name of the database table from the name of your model class and the app that contains it. A model’s database table name is constructed by joining the model’s “app label” – the name you used in manage.py startapp – to the model’s class name, with an underscore between them.

For example, if you have an app bookstore (as created by manage.py startapp bookstore), a model defined as class Book will have a database table named bookstore_book.

To override the database table name, use the db_table parameter in class Meta.

You don't need to know the table name, if you use the API like you have listed:

Entry.objects.filter(pub_date__year=2007).update(headline='Everything is the same')

Django will know what table to update, because of the model you have referenced.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks for ur response. Now i understood. then these are (blog ,headline, body_text, pub_date, ..etc) column names of the table is it??
thanks. Please one more doubt if need to fetch data more from than one table means, at that time we have to write stored procedure and have to call the procedure in it right??
1

The name of table is determined by model parameter "table_name". If you don't specified it, django automatically derives the name of the database table from the name of your model class and the app that contains it: https://docs.djangoproject.com/en/dev/ref/models/options/#db-table

If your question is about performing SQL queries in django, this will be helpful: https://docs.djangoproject.com/en/dev/topics/db/sql/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.