• Home
  • Django
  • django create object with dictionary and field value

django create object with dictionary and field value

linkaiyi
Follow

create django object with field value and dictionary

django_model.objects.create(field1=value1, field2=value2, **dict)

You can use the place_id instead, this is the “twin field” Django creates that is stored in the database, and stores a reference (usually the primarh key) to the target model, given of course such Place already exists:

user_details = [
    UserDetails(name=record['name'], place_id=record['place'])
    for record in list_of_records
]
UserDetails.object.bulk_create(user_details)

Here the list_of_records is thus your list of dictionaries. We can make use of .bulk_create(…) [Django-doc] to add all UserDetails in a small number of insert queries.

Object has 0 attachments

Was this article helpful?

This article is viewed 8 times!

Recent Viewed Articles

Related Articles

0 Comments

Leave a Comment

Support