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
Related
I also need the field (commentGroupDesc) from the foreign keys objects.
models.py
class commentGroup (models.Model):
commentGroup = models.CharField(_("commentGroup"), primary_key=True, max_length=255)
commentGroupDesc = models.CharField(_("commentGroupDesc"),null=True, blank=True, max_length=255)
def __str__(self):
return str(self.commentGroup)
class Meta:
ordering = ['commentGroup']
class Comment (models.Model):
commentID = models.AutoField(_("commentID"),primary_key=True)
commentUser = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
commentGroup = models.ForeignKey(commentGroup, on_delete=models.CASCADE, null=True)
commentCI = models.ForeignKey(Servicenow, on_delete=models.CASCADE, null=True)
commentText = RichTextField(_("commentText"), null=True, blank=True)
commentTableUpdated = models.CharField(_("commentTableUpdated"), null=True, blank=True, max_length=25)
def __str__(self):
return str(self.commentGroup)
class Meta:
ordering = ['commentGroup']
views.py
comment = Comment.objects.get(pk=commentID)
Here I get the commentGroup fine but I also need commentGroupDesc to put into my form.
At first, it's not a good thing to name same your model field as model name which is commentGroup kindly change field name, and run migration commands.
You can simply use chaining to get commentGroupDesc, also it's better to use get_object_or_404() so:
comment = get_object_or_404(Comment,pk=commentID)
group_desc = comment.commentGroup.commentGroupDesc
Remember to change field and model name first.
I'm trying to save unique name in the database but the problem I can save the same with different letters, for example I can save (IT, it, iT, It) I don't want to save it like that.
Model:
class Service(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
name = models.CharField(max_length=127, unique=True, null=False, blank=False) # that field
is_active = models.BooleanField(default=True)
is_deleted = models.BooleanField(default=False)
created_at = models.DateTimeField(auto_now_add=True)
created_by = models.ForeignKey(
"accounts.User",
on_delete=SET_NULL,
blank=False,
null=True,
related_name="service_created_by",
)
def __str__(self):
return f"{self.name}"
A very simple solution:
class Service(models.Model):
name = models.CharField(max_length=50, unique=True)
....
def clean(self):
self.name = self.name.capitalize()
this one helped me
class Service(models.Model):
name = models.CharField(max_length=50, unique=True, null=False, blank=False)
....
class Meta:
constraints = [
models.UniqueConstraint(Lower("name"), name="unique_name"),
]
def clean(self):
self.name = self.name.capitalize()
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)
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 want to find the difference between quantity of Product class and sold_quantity of Stock class. How do I do that
models.py
from django.db import models
class Category(models.Model):
name = models.CharField(max_length=100, blank=True, null=True)
def __str__(self):
return self.name
class Product(models.Model):
name = models.CharField(max_length=100)
slug = models.CharField(max_length=100)
price = models.DecimalField(max_digits=6, decimal_places=2)
quantity = models.IntegerField(null=True, blank=True)
category = models.ForeignKey(
Category, on_delete=models.CASCADE, blank=True, null=True)
def __str__(self):
return self.name
class Stock(models.Model):
sold_quantity = models.IntegerField(null=True, blank=True)
product = models.ForeignKey(Product, on_delete=models.CASCADE, null=True)
def __str__(self):
return self.product.name
For one instance you can do this by:
Example: id=1
product_instance = Product.objects.get(id=1)
quantity_difference = product_instance.quantity - product_instance.stock.sold_quantity
Note: This assumes the relationship is coupled between models.