How to add Data to single filed in Django through shell ?

Hi now i added a new column field below (shop FIeld) and did all the migrations part/command

class retailers(models.Model):

links = models.CharField(max_length=2000,unique=True)

shop = models.TextField(blank = True)

title = models.CharField(max_length=300)

price = models.DecimalField(max_digits=10, decimal_places=2)

now i tried the following python manage.py shell:

from prods.models import retailers

K = Retailers(shop = "amazon")

K.save()

But i get this error, Please Help

IntegrityError: null value in column “price” of relation “prods_retailers” violates not-null constraint

DETAIL: Failing row contains (90, , , , , null, , , 2021-11-29 14:50:47.248006+00, -2, amazon).

Are you trying to add a new row or just change the existing data?

I know this doesn’t help your overall goal, but that message means you’re trying to put a NULL in a not nullable DB field. So you need to make sure you’re pumping data into all fields that cannot be NULL.

But I feel like you have a bigger goal in mind, and I think we need to know that before giving any actual advice.

hi the other fields already consist data, i just added new Shops field.

and now want to just add data to shops field without disturbing other data fields

hi the other fields already consist data, i just added new Shops field.

and now want to just add data to shops field without disturbing other data fields

Ok, so you’re looking to update existing rows.

See the docs for the update method.

hi so i tried the following but it does not work.

from prods.models import retailers`

R = retailers.objects.all()

for all in R:`

  all.shop= "amazon"

  all.save()

Where / how are you running this? Are you running this from the Django shell?

(Side note: I recommend you start using Django’s coding conventions. Class names should be capitalized, variables lower-cased. It’ll help avoid confusion.)

2 Likes

hey thanks it worked

retailers.objects.filter(shop = "").update(shop = "amazon")