Why cannot I migrate. It says assertion error. Here is the pic
# Create your models here.
class Product(models.Model):
product_id= models.AutoField()
product_name = models.CharField(max_length=50)
category = models.CharField(max_length=50, default="" )
subcategory = models.CharField(max_length=50, default="" )
price = models.IntegerField(default=0)
desc = models.CharField(max_length=300)
pub_date = models.DateField()
image = models.ImageField(upload_to='shop/images', default="")
def __str__(self):
return self.product_name
It conflicted product_id field within Product Table. Or if you want to create product_id as your primary key in your product table just write models.AutoField(primary_key=True)
Related
Im working in my Python Django ecommerce project.Now i have 2 classes Item and BookItem :
class Item(models.Model):
item_name = models.CharField(max_length=200, unique=True)
slug = models.SlugField(max_length=200,unique=True)
description = models.TextField(max_length=500, blank=True)
price = models.IntegerField()
images = models.ImageField(upload_to='photos/products')
stock = models.IntegerField()
is_available = models.BooleanField(default = True)
category = models.ForeignKey(Category,on_delete = models.CASCADE)
created_date = models.DateTimeField(auto_now=True)
modified_date= models.DateTimeField(auto_now=True)
def __str__(self):
return self.item_name
def get_url(self):
return reverse('item_detail',args=[self.category.slug, self.slug])
class BookItem(Item):
book = models.ForeignKey(Book,on_delete = models.CASCADE)
I want to get BookItem data from the class Item.Anyone knows how to do it ?
Get Book data for all Items
Item.objects.all().values(
"book__book_name",
"book__import_price",
# "class_name__field_name", structure of the query
)
To filter Item data using Book fields
[filter Items where book name is 'bookname']
Item.objects.filter(book__book_name="bookname")
I just created a model like this :
from django.db import models
# Create your models here.
class Category:
title = models.CharField(max_length=50)
slug = models.CharField(max_length=200)
cat_image = models.ImageField(upload_to='images/')
def __str__(self):
return self.title
class Brand:
title = models.CharField(max_length=50)
slug = models.CharField(max_length=200)
brand_image = models.ImageField(upload_to='images/')
def __str__(self):
return self.title
class UOM:
title = models.CharField(max_length=100)
def __str__(self):
return self.title
class Product_Images:
multi_images = models.ImageField(upload_to='images/')
class Product:
name = models.CharField(max_length=100)
slug = models.CharField(max_length=200)
category = models.ForeignKey(Category, on_delete=models.CASCADE)
brand = models.ForeignKey(Brand, on_delete=models.CASCADE)
price = models.IntegerField(null=True, blank=True)
height = models.IntegerField(null=True, blank=True)
weight = models.IntegerField(null=True, blank=True)
length = models.IntegerField(null=True, blank=True)
color = models.IntegerField(null=True, blank=True)
stock = models.BooleanField()
SKU = models.CharField(max_length=150)
def __str__(self):
return self.name
class Customer:
phone_number = models.CharField(max_length=11)
first_name = models.CharField(max_length=10)
last_name = models.CharField(max_length=10)
email = models.CharField(max_length=20)
password = models.CharField(max_length=10)
def __str__(self):
return self.first_name
class Order:
customer = models.ForeignKey(Customer, on_delete=models.CASCADE)
invoice = models.CharField(max_length=16, null=True, blank=True)
phone = models.CharField(max_length=12)
address = models.CharField(max_length=150)
But facing this error dont have any idea why i am getting this kind of error. I cant even makemigrations if i delete some fields. It says no change detected but i changed some fields still getting this kind of error can some one solve this issue for me. and please explain why ia magetting this kind of error i cant find any problem here. I am also facing this no changes issue on my macos also. i am changing some fields still it says no changes detected .
Models in django haveto be like:
class ModelClass(models.Model):
I'm trying to create a shopping cart model, I've created the Order Model, OrderItem model and Also the Item model. Although I find it difficult to link the order it model to the order in the API view.
here's the Model
class Pizza(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=60)
desc = models.CharField(max_length=150)
price = models.IntegerField()
url = models.CharField(max_length=250)
def __str__(self):
return self.name
class OrderItem(models.Model):
id = models.AutoField(primary_key=True)
product = models.ForeignKey("Pizza", on_delete=models.CASCADE, null=True)
date_added = models.DateTimeField(auto_now=True)
date_ordered = models.DateTimeField(null=True)
amount = models.IntegerField(null=True)
def __str__(self):
return self.product.name
class Order(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=60)
email = models.CharField(max_length=150)
address = models.CharField(max_length=150)
total = models.IntegerField()
created_at = models.DateTimeField(default=timezone.now)
items = models.ManyToManyField(OrderItem)
def __str__(self):
return self.name
//The viewset
class OrderViewSet(viewsets.HyperlinkedModelSerializer):
# permission_classes = [IsPostOrIsAuthenticated,]
serializer_class = OrderSerializer
queryset = Order.objects.all().order_by('-created_at')
Currently, I cant display the amount attribute of the order item in my API, It just shows the id and Also the link to the individual orderitem in an order
I think for cart you can use external fields on relations between Order and Pizza. Documentation about this here
Your models looks like:
class Pizza(models.Model):
name = models.CharField(max_length=60)
desc = models.CharField(max_length=150)
price = models.IntegerField()
url = models.CharField(max_length=250)
def __str__(self):
return self.name
class Order(models.Model):
name = models.CharField(max_length=60)
email = models.CharField(max_length=150)
address = models.CharField(max_length=150)
total = models.IntegerField()
created_at = models.DateTimeField(auto_now=True)
items = models.ManyToManyField(Pizza, through='OrderItem')
def __str__(self):
return self.name
class OrderItem(models.Model):
product = models.ForeignKey('Pizza', on_delete=models.CASCADE, null=True)
order = models.ForeignKey('Order', on_delete=models.CASCADE, null=True)
date_added = models.DateTimeField(auto_now=True)
date_ordered = models.DateTimeField(null=True)
amount = models.IntegerField(null=True)
def __str__(self):
return self.product.name
Example, how you can get dict of items amount in order:
order = Order.objects.filter('-created_at').first() // get last order
cart = {item.pk: item.amount for item in order.items}
I was doing the project on Django to get into it more deeply. I have a problem in the model part. There are models; Company, Product, Category. Company is simply the introduction part. Product is about what product a company has and its divided into category which is ManyToManyField.
I have list all the categories in a page where if certain category is clicked then the list of companies that has product of that category should be filtered. But I don't know how to access it.
Here is my model
class Category(models.Model):
name = models.CharField(max_length=50)
slug = models.SlugField(max_length=50, unique=True)
class Product(models.Model):
name = models.CharField(max_length=200, unique=True, blank=False, null=False)
company = models.ForeignKey('Company', related_name='products', blank=True, null=True, on_delete=models.SET_NULL)
website = models.URLField(unique=True)
slug = models.SlugField(unique=True)
categories = models.ManyToManyField(Category, related_name='products')
class Company(models.Model):
name = models.CharField(max_length=200, unique=True, blank=False, null=False)
slug = models.SlugField(unique=True)
description = models.CharField(max_length=400)
editor = models.ForeignKey(User, related_name='company')
# product = models.ForeignKey(Product, related_name='company')
You can apply the filter:
Company.objects.filter(products__categories__slug=category_slug)
where category_slug it is the value of your current select Category.slug
details:
1) if we need to get Category
cat = Category.objects.get(slug=category_slug)
2) if we need get all Product in this category
product_list = Product.objects.filter(categories=cat)
or add double underscore to many2many models field name
product_list = Product.objects.filter(categories__slug=cat)
# ^^^^
3) if need get Company for the product_list, filter by related_name of related model Product in the field FK on the Company company = models.ForeignKey('Company', related_name='products'
Company.objects.filter(products__in=product_list)
# ^^^^^^^
or by category instance
cat = Category.objects.get(slug=category_slug)
Company.objects.filter(products__categories=cat)
or finaly by category slug value
Company.objects.filter(products__categories__slug=category_slug)
How do i make a subcategory for django.
I want something like this Question -> year -> Mathematic
this is my model
class Category(models.Model):
name = models.CharField(max_length=50, unique=True)
def __unicode__(self):
return self.name
class PastQuestion(models.Model):
category = models.ForeignKey(Category)
subject = models.CharField(max_length=128)
question = models.TextField()
posted_by = models.CharField(max_length=50)
def __unicode__(self):
return self.subject`
Create a foreign key to itself:
class Category(models.Model):
...
parent_category = models.ForeignKey('self', null=True, blank=True)
Then, you can assign any existing Category instance as the parent_category of that instance. Furthermore, if you wanted to find all of the subcategories of a given Category instance you would do something like:
subcategories = Category.objects.filter(
parent_category__id=target_category.id)
class Category( models.Model ):
Title = models.CharField( max_length=255, blank=True, null=False, )
parents = models.ForeignKey('self',limit_choices_to = {'parents__isnull': True}, on_delete=models.CASCADE,default=1, blank=True, null=True, )
try