5

I thought this would have been a previously asked question, so maybe I'm not phrasing it right.

I tried:

manage.py python3.6 dbshell

and then:

 obj= Person.objects.create('Justin')

but this did not work. Thanks for any help.

justin
  • 553
  • 1
  • 7
  • 18
  • 1
    You should use *named* parameters, so something like `Person.objects.create(name='Justin')`, how else is Django supposed to know what field `'Justin'` maps to? – Willem Van Onsem Aug 13 '18 at 18:16
  • I thought if the parameter names weren't supplied it saves in the order listed in the model? – justin Aug 13 '18 at 18:17
  • 1
    There is no order of attributes. The attributes are passed in a dictionary format, so the order is lost. It would also be horrible unstable, since by constructing a new field, then all the constructors should be updated. – Willem Van Onsem Aug 13 '18 at 18:18

3 Answers3

8

You are on the right track, but when you create model instances, you should use named parameters for the fields, so for example:

obj = Person.objects.create(name='Justin')

(given of course a Person has a name field)

This is logical since a model can have several fields, and there is no "inherent" order.

Using positional parameters would be very risky, since a simply "reshuffle" of the fields would result in model object constructions going wrong.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
4

dbshell command runs the command-line client for the database, so you'd have to use SQL to create a row in your database using that. What you actually want is the shell command. It opens a Python interpreter with Django configured, so you can work with ORM there.

mateuszb
  • 1,083
  • 10
  • 20
1
obj= Person.objects.create(name='Justin')
Toby
  • 57
  • 9
Susaj S N
  • 960
  • 1
  • 10
  • 22