3

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

Dean Christian Armada
  • 6,724
  • 9
  • 67
  • 116

1 Answers1

6

Simply unwrap the dictionary within create function like:

Personnels.objects.create(**data)
Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126
  • i am facing the problem when i used this due to presence of a foreign key https://stackoverflow.com/questions/69349618/trying-to-save-dict-in-my-model-but-getting-error-due-to-foreign-key/69355144#69355144 – justjokingbro Sep 28 '21 at 04:50