create new objects using class inmodels

Hello I’m new to django however i Have an issues with inserting data into database so it can appear in admin panel i tried to use the model.objects.create(data…) but it only work when i execute it in same file but when i try to use it in different file that I create notthing happened and no data inserted that my file

from django.db import models

class Product(models.Model):
    name = models.CharField(max_length=50)
    age =  models.IntegerField()
    content =  models.TextField(max_length=400)
    def __str__(self):

and this my file called in same folder of

from models import Product
data = {
    "content":"dont waste your money"
# Use the imported model

You have a couple different options for doing this “properly”.

  • Insert the data directly into the tables using your database’s command line tool. (e.g. Sqlite3 for Sqlite, psql for PostgreSQL, etc)

  • Add the data to the table using the Django admin.

  • Load the data as a fixture using the loaddata command.

  • Write a custom admin command to create the objects and save them in the database.

What you don’t want to do is to try and do it by adding a module to your code like you’re trying here.

1 Like

i think i’ll ended using sql since my crawler is a bit large and do many jobs, i do not want to mix it with my django app , but i really hoped that i can insert data directly using file :cry: :cry: :cry: :cry: ,

It doesn’t make sense to do that. Your file is loaded every time your app is initialized. But you only want to load this data once. (Do you really want hundreds of copies of the same data in your database?)

1 Like