Lilly Mayo-099
Lilly Mayo-099

Reputation: 59

Creating an object in django

I have just started learning Django and I am making a database using SQLite I have a python file called models.py and I am currently creating a py file for the objects called object. However I am having trouble creating objects in the database I have made. I think there is an issue with the object.py file because it is claiming that my FruitInfo sections have no objects.

models.py
from django.db import models
from django.core.validators import MinLengthValidator, MaxValueValidator, MinValueValidator

class FruitInfo(models.Model):
    id = models.IntegerField(primary_key=True, max_length=30, validators=[MinLengthValidator("5")])
    name= models.CharField(max_length=30)
    origin = models.CharField(max_length=60)
    price = models.DecimalField(max_digits=4,null=False,decimal_places=2, validators=[MinValueValidator('200')])
 
    def __str__(self):
            return self.origin + " " + self.name

object.py
from stinky.models import FruitInfo
FruitInfo.objects.all()
FruitInfor.objects.all().values()

record = FruitInfo.objects.create(id = "A0001", name = "Pink Lady Apple", origin= "Washington State", price="$2.00/kg" )
record = FruitInfo.objects.create(id = "A0002", name = "Organic Bananana", origin = "Australia", price = "$4.50/kg")
record = FruitInfo.objects.create(id = "A0003", name = "Orange", origin = "Indonesia", price = "$4/kg")

record.save()

Upvotes: 1

Views: 1591

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476567

I am currently creating a py file for the objects called object.

object.py is just a "simple" python file. Unless you load it somewhere, it will not run. But even if it does, this is not the way to construct objects in the database: this means it will each time you start the server, it will try to construct new objects. It also means that if you later alter that model, that you each time will have to update the logic of that file.

Typically one populates the database like that through a data migration [Django-doc].

You can create such migration by creating an empty migration file with:

python3 manage.py makemigrations --empty stinky

With stinky, the name of the app.

Then we can create data in the migration by altering the migration file to something like:

from django.db import migrations

def populate_database(apps, schema_editor):
    FruitInfo = apps.get_model('stinky', 'FruitInfo')
    FruitInfo.objects.create(id='A0001', name='Pink Lady Apple', origin='Washington State', price='$2.00/kg')
    FruitInfo.objects.create(id='A0002', name='Organic Bananana', origin='Australia', price='$4.50/kg')
    FruitInfo.objects.create(id='A0003', name='Orange', origin='Indonesia', price='$4/kg')


class Migration(migrations.Migration):

    dependencies = [
        ('stinky', '1234_some_migration'),
    ]

    operations = [
        migrations.RunPython(populate_database),
    ]

The apps.get_model('stinky', 'FruitInfo') will fetch a model how it looks at that specific time in the migrations. This thus means that if you later for example add extra fields, then the records will run through that other migration, but the FruitInfo here will not have that extra field.

Upvotes: 2

Related Questions