Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update a foreign key field in Django models.py?

Here is the source: https://docs.djangoproject.com/en/1.8/topics/db/queries/

At the beginning, let's look at the models.py field:

from django.db import models

class Blog(models.Model):
    name = models.CharField(max_length=100)
    tagline = models.TextField()

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

class Author(models.Model):
    name = models.CharField(max_length=50)
    email = models.EmailField()

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

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

This is how, a row or an object is created of Blog type:

>>> from blog.models import Blog
>>> b = Blog(name='Beatles Blog', tagline='All the latest Beatles news.')
>>> b.save()

My question is how to update blog column/attribute of Entry object (which stores Foreign Key in relation to Blog) with the entry I created in Blog class?

like image 885
Asif Avatar asked Sep 20 '17 05:09

Asif


People also ask

How do I update a specific field in Django?

Use update_fields in save() If you would like to explicitly mention only those columns that you want to be updated, you can do so using the update_fields parameter while calling the save() method. You can also choose to update multiple columns by passing more field names in the update_fields list.

How do I change the ForeignKey in Django REST framework?

As stated in the documentation, you will need to write your own create() and update() methods in your serializer to support writable nested data. You will also need to explicitly add the status field instead of using the depth argument otherwise I believe it won't be automatically added to validated_data .

How do you add a new field to a model with new Django migrations?

To answer your question, with the new migration introduced in Django 1.7, in order to add a new field to a model you can simply add that field to your model and initialize migrations with ./manage.py makemigrations and then run ./manage.py migrate and the new field will be added to your DB.

Does Django automatically index foreign keys?

Django automatically creates an index for all models. ForeignKey columns. From Django documentation: A database index is automatically created on the ForeignKey .


1 Answers

First get the Blog object and then pass it to Entry.

b=Blog.objects.get(name='Beatles Blog', tagline='All the latest Beatles news.')

e=Entry(blog=b,headline='some headline',body_text='text here',....)
e.save()
like image 50
Astik Anand Avatar answered Nov 15 '22 08:11

Astik Anand