How to show name in foreign key? - python

In Models:
class ShopCategory(models.Model):
category = models.CharField(max_length=100)
class Meta:
db_table = "tbl_ShopCategory"
class Shop(models.Model):
id_shop = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
name = models.CharField(max_length=200)
slug = models.SlugField(max_length=250, blank=True, null=True)
cover_img = models.CharField(max_length=250, blank=True)
category = models.ForeignKey(ShopCategory, on_delete=models.CASCADE)
avgscore = models.FloatField(default=0)
I want to show the Category name?
...........................................................

Add an str method to your ShopCategory model:
class ShopCategory(models.Model):
category = models.CharField(max_length=100)
def __str__(self):
return self.category
class Meta:
db_table = "tbl_ShopCategory"

Related

Python/Django MakeMigrations Error Traceback

Here is what appears when i try to use python manage.py makemigrations
traceback error
Here is models.py code
----------------------code-------------------------------------
from django.db import models
from django.contrib.auth.models import User
class Category(models.Model):
name = models.CharField(max_length=255, db_index=True)
slug = models.SlugField(max_length=255, unique=True)
class Meta:
verbose_name_plural = 'categories'
# def get_absolute_url(self):
# return reverse("store:category_list", args=[self.slug])
def __str__(self):
return self.name
class Product(models.Model):
category = models.ForeignKey(Category, related_name='product', on_delete=models.CASCADE)
created_by = models.ForeignKey(User, on_delete=models.CASCADE, related_name='product_creator')
title = models.CharField(max_length=255)
author = models.CharField(max_length=255,default='admin')
description = models.TextField(blank=True)
image = models.ImageField(upload_to='images/')
slug = models.SlugField(max_length=255)
price = models.DecimalField(max_digits=4, decimal_places=2)
in_stock = models.BooleanField(default=True)
is_active = models.BooleanField(default=True)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
class Meta:
verbose_name_plural = 'Products'
ordering = ('-created',)
def __str__(self):
return self.title
------------------code end-------------------------------
im new and i dont know whats the problem

How can I filter the field so that when I select a category, only those products that belong to this category are displayed?

My models, category and product. Each product has a category field, which is linked through ForeignKey.
class Category(models.Model):
name = models.CharField(max_length=50, unique=True)
description = models.TextField()
image = models.ImageField(upload_to='category', blank=True)
class Meta:
verbose_name = 'category'
verbose_name_plural = 'categories'
def __str__(self):
return self.name
class Product(models.Model):
name = models.CharField(max_length=50, unique=True)
description = models.TextField()
category = models.ForeignKey(Category, on_delete=models.CASCADE, related_name='category')
price = models.DecimalField(max_digits=10, decimal_places=2)
image = models.ImageField(upload_to='product', blank=True)
stock = models.PositiveIntegerField()
available = models.BooleanField(default=True)
created = models.DateTimeField(auto_now_add=True)
update = models.DateTimeField(auto_now=True)
class Meta:
verbose_name = 'product'
verbose_name_plural = 'products'
def __str__(self):
return self.name
And views, a product category can be selected and when I click on a category, I want the product to appear only in that category
class CategoriesList(LoginRequiredMixin, ListView):
login_url = 'login/'
template_name = 'index.html'
model = Category
class ProductsList(ListView):
template_name = 'products.html'
model = Product
def get_queryset(self):
return super().get_queryset().filter(category=category_id)

Problem with Django project. AttributeError at / 'Product' object has no attribute 'Category'

Hello I was wondering could anyone help me with an issue I've ran into while working on a Django project. I've tried a few different things but cant seem to get it working a screenshot of the error is attached.
[![enter image description here][1]][1]
error
[1]: https://i.stack.imgur.com/LKTPu.png
import uuid
from django.db import models
from django.urls import reverse
class Category(models.Model):
id = models.UUIDField(
primary_key=True,
default=uuid.uuid4,
editable=False)
name = models.CharField(max_length=250, unique=True)
description = models.TextField(blank=True)
image = models.ImageField(upload_to='category', blank=True)
class Meta:
ordering = ('name',)
verbose_name = 'category'
verbose_name_plural = 'categories'
def get_absolute_url(self):
return reverse('phoneshop:products_by_category', args=[self.id])
def __str__(self):
return self.name
class Product(models.Model):
id = models.UUIDField(
primary_key=True,
default=uuid.uuid4,
editable=False)
name = models.CharField(max_length=250, unique=True)
description = models.TextField(blank=True)
category = models.ForeignKey(Category, on_delete=models.CASCADE)
price = models.DecimalField(max_digits=10, decimal_places=2)
image = models.ImageField(upload_to='product', blank=True)
stock = models.IntegerField()
available = models.BooleanField(default=True)
created = models.DateTimeField(auto_now_add=True, blank=True, null=True)
updated = models.DateTimeField(auto_now_add=True, blank=True, null=True)
class Meta:
ordering = ('name',)
verbose_name = 'product'
verbose_name_plural = 'products'
def get_absolute_url(self):
return reverse('phoneshop:prod_detail', args=[self.Category.id, self.id])
def __str__(self):
return self.name

Find the difference between two model field

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.

Django migration error,auth_permission_content_type_id_codename_01ab375a_uniq duplicated key

I am trying to migrate my module to PostgresSQL but when I do I got an error that teling me the key unique "auth_permission_content_type_id_codename_01ab375a_uniq" is duplicated .
the key (content_type_id, codename)=(1,view_arrets) are existed .
The code somehow create a duplicates views .
my modules are :
from django.db import models
from django.urls import reverse
class ProfilsHoraires(models.Model):
P_id = models.AutoField(primary_key=True)
id_profil = models.CharField(unique=True,max_length=120)
d_p_1 = models.TimeField()
f_p_1 = models.TimeField()
d_p_2 = models.TimeField()
f_p_2 = models.TimeField()
class Meta:
db_table = 'profilsHoraires'
ordering = ['P_id']
def get_absolute_url(self):
return reverse("profiles-edit", kwargs={"P_id": self.P_id})
class categorie(models.Model):
Categorie = models.CharField(primary_key=True,max_length=20, blank=True)
class Meta:
db_table = 'categorie'
ordering = ['Categorie']
def get_absolute_url(self):
return reverse("categorie-edit", kwargs={"id": self.Categorie})
class Destination(models.Model):
destinationNom=models.CharField(primary_key=True,max_length=90, blank=True)
class Meta:
db_table = 'disination'
ordering = ['destinationNom']
def get_absolute_url(self):
return reverse("destination-edit", kwargs={"id": self.destinationNom})
class Collaborateurs(models.Model):
mle = models.AutoField(primary_key=True)
nom = models.CharField(max_length=90, blank=True, null=True)
prenom = models.CharField(max_length=90, blank=True, null=True)
address = models.TextField(blank=True, null=True)
latitude = models.FloatField(blank=True, null=True)
longtitude = models.FloatField(blank=True, null=True)
libele_dest = models.ForeignKey(Destination, on_delete=models.SET_NULL, null=True)
profil = models.ForeignKey(ProfilsHoraires, on_delete=models.SET_NULL, null=True)
class Meta:
db_table = 'collaborateurs'
ordering = ['mle']
def __unicode__(self):
return self.title
def get_absolute_url(self):
return reverse("collabs-edit", kwargs={"mle": self.mle})
class Vehicules(models.Model):
V_id = models.AutoField(primary_key=True)
allocation= models.FloatField(null=False)
capacite = models.IntegerField(null=False)
type = models.CharField(max_length=20, blank=True, null=False)
class Meta:
db_table = 'vehicules'
def __unicode__(self):
return self.type
def get_absolute_url(self):
return reverse("vehicules-edit", kwargs={"V_id": self.V_id})
class LieuxAffet(models.Model):
id_lieu = models.AutoField(primary_key=True)
id_ville = models.IntegerField(null=True)
libelle_lieu = models.TextField(null=True)
latitude = models.FloatField(null=True)
longtitude = models.FloatField(null=True)
libelle_dest = models.ForeignKey(Destination, on_delete=models.SET_NULL, null=True)
class Meta:
db_table = 'lieux_affet'
def get_absolute_url(self):
return reverse("lieux-edit", kwargs={"id_lieu": self.id_lieu})
class Arrets(models.Model):
id_arret = models.IntegerField(primary_key=True)
id_lieu_affet = models.IntegerField(null=True)
id_dest = models.IntegerField(null=True)
libelle_arret = models.TextField(null=True)
latitude = models.FloatField(null=True)
longtitude = models.FloatField(null=True)
id_suiv = models.IntegerField(null=True)
class Meta:
db_table = 'Arrets'
def get_absolute_url(self):
return reverse("arrtes-edit", kwargs={"id_arret": self.id_arret})

Categories