Updating django database

39 views
Skip to first unread message

Albin Antony

unread,
May 29, 2018, 7:41:29 AM5/29/18
to django...@googlegroups.com
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)


Anthony Flury

unread,
Jun 2, 2018, 8:50:47 AM6/2/18
to django...@googlegroups.com
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.
> --
> 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...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@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/CAEohp0dkVzwRZWe9ig8cWWVyXq08K_qndfVmSpj8AM9Zxgaf-Q%40mail.gmail.com
> <https://groups.google.com/d/msgid/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 : *Anthon...@btinternet.com*
Twitter : *@TonyFlury <https://twitter.com/TonyFlury/>*

Albin Antony

unread,
Jun 2, 2018, 9:12:53 AM6/2/18
to django...@googlegroups.com
For updating multiple models at the same time, Should we create multiple instances?

To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com <mailto:django-users+unsubscrib...@googlegroups.com>.
To post to this group, send email to django...@googlegroups.com <mailto:django-users@googlegroups.com>.


--
--
Anthony Flury
email : *Anthon...@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+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.

Melvyn Sopacua

unread,
Jun 2, 2018, 10:19:46 AM6/2/18
to django...@googlegroups.com
On zaterdag 2 juni 2018 15:12:08 CEST Albin Antony wrote:
> For updating multiple models at the same time, Should we create multiple
> instances?

Multiple models (Patient and Medication) or multiple instances of the same
model (more than one Patient)?

The later can be done with a single manager operation. The first cannot.

--
Melvyn Sopacua

Albin Antony

unread,
Jun 2, 2018, 10:27:57 AM6/2/18
to django...@googlegroups.com
Can the first be done if there is a relation between the models?


--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Anthony Flury

unread,
Jun 2, 2018, 6:02:41 PM6/2/18
to django...@googlegroups.com
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 instance


On 29/05/18 12:37, Albin Antony wrote:
> --
> 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...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@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/CAEohp0dkVzwRZWe9ig8cWWVyXq08K_qndfVmSpj8AM9Zxgaf-Q%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CAEohp0dkVzwRZWe9ig8cWWVyXq08K_qndfVmSpj8AM9Zxgaf-Q%40mail.gmail.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.


liujiawe...@bytedance.com

unread,
Jun 3, 2018, 7:37:44 AM6/3/18
to Django users
I think you can use model.object.get_or_create()
and like this:
   patient,err = Patient.obiect.get_or_create(kwgs)
   if the object existed,the returned will be wrong, if not ,it will be true,
and then you update this instence.

在 2018年5月29日星期二 UTC+8下午7:41:29,Albin Antony写道:
Reply all
Reply to author
Forward
0 new messages