For updating multiple models at the same time, Should we create multiple instances?
On Sat, Jun 2, 2018 at 6:19 PM, 'Anthony Flury' via Django users < django-users@googlegroups.com> wrote: > Is this a new or a update from the front end ? > > If new : > 1 Extract json using : data = json.load( request.POST['json'] - this > converts data to a dictionary (assuming that the json is in the 'json' > field in the message. > 2 Validate as necessary > 3 Create instance : instance = Patient.create(**data), > instance.save() > > if an update to an existing instance : > 1 Extract json using : data = json.load( request.POST['json'] - this > converts data to a dictionary (assuming that the json is in the 'json' > field in the message. > 2 Validate as necessary > 3 Update : > for field, value in data.items(): > setattr(instance, field, value) > > instance.save() > > Assuming that instances is the Patient record to be updated. > > I hope this helps. > > > > On 29/05/18 12:37, Albin Antony wrote: > >> Hello guys, >> Sorry about the above no subject mail >> I am getting a json string(json_string) from frontend. How can we update >> the django database in views.py. Below is my models.py. >> models.py >> >> class Patient(models.Model): >> patient_id = models.CharField(primary_key=True, max_length=200) >> patient_age = models.CharField(max_length=200) >> patient_name = models.CharField(max_length=200) >> patient_refby = models.CharField(max_length=200) >> patient_gender = models.CharField(max_length=20, >> default=Gender.UNKNOWN.value) >> >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Django users" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to django-users+unsubscr...@googlegroups.com <mailto: >> django-users+unsubscr...@googlegroups.com>. >> To post to this group, send email to django-users@googlegroups.com >> <mailto:django-users@googlegroups.com>. >> Visit this group at https://groups.google.com/group/django-users. >> To view this discussion on the web visit https://groups.google.com/d/ms >> gid/django-users/CAEohp0dkVzwRZWe9ig8cWWVyXq08K_ >> qndfVmSpj8AM9Zxgaf-Q%40mail.gmail.com <https://groups.google.com/d/m >> sgid/django-users/CAEohp0dkVzwRZWe9ig8cWWVyXq08K_ >> qndfVmSpj8AM9Zxgaf-Q%40mail.gmail.com?utm_medium=email&utm_source=footer >> >. >> For more options, visit https://groups.google.com/d/optout. >> > > > -- > -- > Anthony Flury > email : *anthony.fl...@btinternet.com* > Twitter : *@TonyFlury <https://twitter.com/TonyFlury/>* > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to django-users+unsubscr...@googlegroups.com. > To post to this group, send email to django-users@googlegroups.com. > Visit this group at https://groups.google.com/group/django-users. > To view this discussion on the web visit https://groups.google.com/d/ms > gid/django-users/24980dc2-6173-decd-67b6-871ef0ef9d3f%40btinternet.com. > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAEohp0c2gUaC4hCjoxYVY7DRFopPbF88R4D_2HEzPGizL7FBoQ%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.