31

I'm new in Django. I've created a table by insert model into models.py.

Now, I want to insert a row into the database - table Dodavatel. I know, that I have to create an object with attributes as columns. But I don't know where should I put this code. In models.py?

This is my model:

class Dodavatel(models.Model):
    nazov = models.CharField(default='', max_length=255)
    dostupnost = models.IntegerField(default=0)

This is the code for inserting a row:

p = Dodavatel(nazov='Petr', dostupnost=1)
p.save()

Where should I put this code?

1
  • 1
    Create the database by running a "python manage.py syncdb" first. Then, you can add rows according to your model. Normally this is done in the View functions. Commented May 26, 2014 at 11:23

2 Answers 2

48

If you only want to quick test your models you can start an interactive shell and execute your code there.

python manage.py shell

The above command starts a python interactive shell initialized with your Django project settings.

Then you can do something like:

from your_app_name.models import Dodavatel
p = Dodavatel(nazov='Petr', dostupnost=1)
p.save()

I do not recommend to use that code directly inside a view. Instead to create an item I would use a class based view like CreateView.

Sign up to request clarification or add additional context in comments.

2 Comments

I write a script, which would inserting rows into the database. So this is in my script.py: views.add_Dodavatel("petr", 1) and this is in views.py: def add_Dodavatel(nazov,dostupnost): p = Dodavatel(nazov=nazov, dostupnost=dostupnost) p.save() But when I run the script.py I get error: File "C:\Python27\lib\site-packages\django\db\models\base.py", line 92, in new kwargs = {"app_label": model_module.__name__.split('.')[-2]} IndexError: list index out of range
You should not call a function from your views.py inside a script manually executed. Views should be called by django request-response cycle. If you want to create a script to insert bulk data in your db you should look at: docs.djangoproject.com/en/dev/howto/custom-management-commands
2

For manual inserting data to table for testing purpose

Method1

enter image description here

Execute this command for creating Dodavatel in the database

 python manage.py makemigrations


 python manage.py migrate

enter image description here

Now you can see the table created in the database

enter image description here

Then type for open python project REPL:

python manage.py shell

add this code in the repl

from everych.cheese.models import Dodavatel

 dodavel = Dodavatel.objects.create(nazov="hi", dostupnost=3)

You can use python manage.py shell_plus instead of python manage.py shell.so you didn't want to add the line from everych. cheese. models import Dodavatel it automatically imported.

here everych my project name

cheese is the application name

change as your project and application name

enter image description here

After the above code is executed data will be inserted into the table output:- enter image description here

Method2

Here just inserting data you must add a table using the above step.

python manage.py shell_plus

enter image description here code

dat = Dodavatel(nazov="Petr", dostupnost=1)
dat.save()

enter image description here Output:

enter image description here

1 Comment

manage.py always in the root of the project .so the terminal must be in the root folder

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.