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()
object.pyis just a simple file, it will not run. This is also not the way to add records to the databa. You do this through a data migration: docs.djangoproject.com/en/3.1/topics/migrations/…