Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Model create using dictionary in django

Tags:

python

django

In Django is it possible to create through dictionary similar as to filtering?

Here is my models.py:

class Personnels(models.Model):
    name = models.CharField(max_length=100)

Here is my views.py:

from . models import *

def artists(request):
    personnels = Personnels.objects.all()

    if request.method == "POST":
            data = {"name": "Dean Armada"}
            Personnels.objects.create(data)

However the code above will throw an error. What I really am trying to do is to do a create function coming from a "request.POST" but my views above serves as a simple example on doing it

like image 216
Dean Christian Armada Avatar asked Sep 13 '25 04:09

Dean Christian Armada


1 Answers

Simply unwrap the dictionary within create function like:

Personnels.objects.create(**data)
like image 199
Moinuddin Quadri Avatar answered Sep 15 '25 19:09

Moinuddin Quadri