Dean Christian Armada
Dean Christian Armada

Reputation: 7364

Model create using dictionary in 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

Upvotes: 4

Views: 4238

Answers (1)

Moinuddin Quadri
Moinuddin Quadri

Reputation: 48067

Simply unwrap the dictionary within create function like:

Personnels.objects.create(**data)

Upvotes: 6

Related Questions