Vendor id where to place in ecommerce app Django - django

class Product(models.Model):
title = models.CharField(max_length =120)
slug = models.SlugField(blank=True)
description = models.TextField()
price = models.DecimalField(decimal_places=2, max_digits=20, default=39.99)
image = models.ImageField(upload_to=upload_image_path,null=True, blank=True)
featured = models.BooleanField(default=False)
active = models.BooleanField(default=True)
timestamp = models.DateTimeField(auto_now_add=True)
class OrderItem(models.Model):
item = models.ForeignKey(Product, on_delete=models.CASCADE)
quantity = models.IntegerField(default=1)
item_cart = models.CharField(max_length=20, null=True, blank=True)
class Cart(models.Model):
user = models.ForeignKey(User,null=True, blank=True,on_delete=models.CASCADE)
products = models.ManyToManyField(OrderItem, blank=True)
subtotal = models.DecimalField(default=0.00, max_digits=100, decimal_places=2)
total = models.DecimalField(default=0.00,max_digits=100,decimal_places=2)
updated = models.DateTimeField(auto_now=True)
timestamp = models.DateTimeField(auto_now_add=True)
where to place the vendor id in this model and how to implement for many shops with this datamodel
i did ecom website with this data model for a single shop

Related

Django rest Ecommerce category and product offers

I'm trying to acheive catgeory and product offers in my project and am unable to come up with a solution. Like if i give offer to a category all products price in category should get the offer and for products its individual.
class Category(models.Model):
category_name = models.CharField(max_length=15, unique=True)
slug = models.SlugField(max_length=100, unique=True)
class Meta:
verbose_name_plural = "Category"
class Products(models.Model):
category = models.ForeignKey(Category, on_delete=models.CASCADE)
product_name = models.CharField(max_length=50, unique=True)
slug = models.SlugField(max_length=100, unique=True)
description = models.TextField(max_length=500)
price = models.IntegerField()
images = models.ImageField(upload_to="photos/products")
images_two = models.ImageField(upload_to="photos/products")
images_three = models.ImageField(upload_to="photos/products")
stock = models.IntegerField()
is_available = models.BooleanField(default=True)
created_date = models.DateTimeField(auto_now_add=True)
modified_date = models.DateTimeField(auto_now_add=True)
class Meta:
verbose_name_plural = "Products"
def __str__(self):
return self.product_name
class CategoryOffer(models.Model):
category = models.OneToOneField(Category, on_delete=models.CASCADE, related_name='cat_offer')
valid_from = models.DateTimeField()
valid_to = models.DateTimeField()
discount = models.IntegerField(
validators=[MinValueValidator(1), MaxValueValidator(100)]
)
is_active = models.BooleanField(default=False)
def __str__(self):
return self.category.category_name
class ProductOffer(models.Model):
product = models.OneToOneField(Products, on_delete=models.CASCADE, related_name='pro_offer')
valid_from = models.DateTimeField()
valid_to = models.DateTimeField()
discount = models.IntegerField(
validators=[MinValueValidator(1), MaxValueValidator(100)]
)
is_active = models.BooleanField(default=False)
def __str__(self):
return self.product.product_name
So above are my models. I don't know how to implement, thought of many ways but it keeps leading to errors.
You are using separate models for Categoryoffer and Productoffer. Make an Offer model with the following field:
class Offer:
name = models.CharField()
valid_from = models.DateTimeField()
valid_to = models.DateTimeField()
discount = models.IntegerField(
validators=[
MinValueValidator(1),
MaxValueValidator(100)
])
is_active = models.BooleanField(default=False)
Now use the foreign key in your category and product models:
class Product:
offer = models.ForeignKey(Offer)

I want to select as much as member CustomUser(which is Agents Model instances) while creating a project

Filter A foreignkey child model instance in a form field based on the parent value selected in another field in the same field
class Agents(models.Model):
id = models.AutoField(primary_key=True)
admin = models.OneToOneField(CustomUser, on_delete = models.CASCADE)
address = models.TextField()
gender = models.CharField(max_length=50)
numero_telephone = models.CharField(max_length=50)
departement_id = models.ForeignKey(Departements, on_delete=models.CASCADE, default=1)
role_id = models.ForeignKey(Roles, on_delete=models.CASCADE, default=1)
profile_pic = models.FileField()
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
objects = models.Manager()
class Projets(models.Model):
id = models.AutoField(primary_key=True)
projet_name = models.CharField(max_length=255)
departement_id = models.ForeignKey(Departements, on_delete=models.CASCADE, default=1) #need to give defauult course
manager_id = models.CharField(max_length=255, null=True,blank=True)
membres_id = models.ForeignKey(CustomUser, on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
objects = models.Manager()

django-import-export package get a Foreignkey value while Export to XL file

First pardon me for my bad english. I am very new in Django Framework. I want to Export a foreignkey value called title which is in 'Product' model. There is multiple Foreignkey Relations between different django app models. Please take a look below all of this models:
products/models.py
class Product(models.Model):
title = models.CharField(max_length=500)
brand = models.ForeignKey(
Brand, on_delete=models.CASCADE, null=True, blank=True)
image = models.ImageField(upload_to='products/', null=True, blank=True)
price = models.DecimalField(decimal_places=2, max_digits=20, default=450)
old_price = models.DecimalField(
default=1000, max_digits=20, decimal_places=2)
weight = models.CharField(
max_length=20, help_text='20ml/20gm', null=True, blank=True)
size = models.CharField(
max_length=10, help_text='S/M/L/XL/32/34/36', null=True, blank=True)
active = models.BooleanField(default=True)
timestamp = models.DateTimeField(auto_now_add=True)
update = models.DateTimeField(auto_now=True)
carts/models.py
class Entry(models.Model):
product = models.ForeignKey(Product, null=True, on_delete=models.CASCADE)
cart = models.ForeignKey(Cart, null=True, on_delete=models.CASCADE)
quantity = models.PositiveIntegerField(default=1)
price = models.DecimalField(default=0.00, max_digits=100, decimal_places=2)
last_purchase_price = models.DecimalField(
default=0.0, max_digits=20, decimal_places=15)
weight_avg_cost = models.DecimalField(
default=0.0, max_digits=20, decimal_places=15)
updated = models.DateTimeField(auto_now=True)
timestamp = models.DateTimeField(auto_now_add=True)
class Meta:
ordering = ['-timestamp']
And my working models is analytics/models.py, from where I want to export the product title in XL sheet.
class AllReportModel(models.Model):
# All report model
invoice = models.ForeignKey(Invoice, on_delete=models.CASCADE, null=True, blank=True)
ws_invoice = models.ForeignKey(WholesaleInvoice, on_delete=models.CASCADE, null=True, blank=True)
entry = models.ForeignKey(Entry, on_delete=models.CASCADE, null=True, blank=True)
stock_quantity = models.IntegerField()
timestamp = models.DateTimeField(auto_now_add=True)
update = models.DateTimeField(auto_now=True)
and Finally analytics/admin.py is :
class AllReportModelResource(resources.ModelResource):
class Meta:
model = AllReportModel
fields = ('id', 'invoice', 'ws_invoice', 'entry', 'stock_quantity')
class AllReportModelAdmin(ImportExportModelAdmin):
resource_class = AllReportModelResource
list_display = ['invoice', 'ws_invoice', 'entry', 'timestamp']
search_fields = ['invoice']
admin.site.register(AllReportModel, AllReportModelAdmin)
Help will be highly appreciated.
I've been into this situation before, its solution is very easy.
You can refer to django-import-export documentation for advanced data manipulation on export Click HERE.

Update an instance of a model in django such that the older instance and its relationship with other instances remain unaffected

I have been working on a e-commerce project. I have three models Item, OrderItem, Order. They are linked with Foreignkey(s) (Item -> OrderItem -> Order). Item is the actual product and an Order contain(s) Item(s).
Item basically represents a product. In Item there is an attribute 'price' which needs to updated as need suggest. Like during a sale or something else.
What happens is when I update the price of an Item, the price of that item also gets updated in the instances of the Order(s) that are already completed.
Basically I would want to separate these models in a way such that any changes in the Item model doesn't effect the Orders that are completed.
class Item(models.Model):
title = models.CharField(max_length=100)
sku = models.CharField(max_length=8, validators=[
MinLengthValidator(8)], unique=True)
upc = models.CharField(max_length=12, validators=[
MinLengthValidator(12)], unique=True, blank=True, null=True)
date_updated = models.DateTimeField(
auto_now=True, blank=True, null=True)
price = models.FloatField()
discount_price = models.FloatField(blank=True, null=True)
category = models.CharField(choices=CATEGORY_CHOICES, max_length=2)
label = models.CharField(choices=LABEL_CHOICES, max_length=1)
slug = models.SlugField()
description = models.TextField()
image = models.ImageField(upload_to=upload_location, blank=True, null=True)
stock_quantity = models.IntegerField(default=0)
class OrderItem(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL,
on_delete=models.CASCADE)
ordered = models.BooleanField(default=False)
item = models.ForeignKey(Item, on_delete=models.CASCADE)
item_variations = models.ManyToManyField(ItemVariation)
quantity = models.IntegerField(default=1)
purchase = models.FloatField(blank=True, null=True)
def get_total_item_price(self):
return self.quantity * self.item.price
class Order(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL,
on_delete=models.CASCADE)
ref_code = models.CharField(
max_length=20, blank=True, null=True, unique=True)
items = models.ManyToManyField(OrderItem)
start_date = models.DateTimeField(auto_now_add=True)
# Check this
ordered_date = models.DateTimeField()
# When the payment is made it becomes True
ordered = models.BooleanField(default=False)
shipping_address = models.ForeignKey(
'Address', related_name='shipping_address', on_delete=models.SET_NULL, blank=True, null=True)
billing_address = models.ForeignKey(
'Address', related_name='billing_address', on_delete=models.SET_NULL, blank=True, null=True)
payment = models.ForeignKey(
'Payment', on_delete=models.SET_NULL, blank=True, null=True)
coupon = models.ForeignKey(
'Coupon', on_delete=models.SET_NULL, blank=True, null=True)
being_delivered = models.BooleanField(default=False)
received = models.BooleanField(default=False)
refund_requested = models.BooleanField(default=False)
refund_granted = models.BooleanField(default=False)
refund_refused = models.BooleanField(default=False)
Any help will be appreciated, thank you.
You could have ItemPrice as a separate model with a One-to-Many relationship. Which prices for the item are stored with associated date changed.
models.py
class ItemPrice(models.Model):
item = models.ForeignKey(Item, on_delete=models.CASCADE)
price = models.FloatField()
date_changed = models.DateTimeField(auto_now=True, blank=True, null=True)
Then align your order date with the items price at that current time.

How to get the minimum value of an instance of django model

I am trying to get the minimum or the lowest value of a model field in django model. The field is room_Price. I am therefore trying to get the minimum of value of this field for each instance of a model. My model are as as follows
class Hotels(models.Model):
name = models.CharField(max_length=255)
address = models.CharField(max_length=255)
city = models.CharField(max_length=255)
country = models.CharField(max_length=255)
mobile_number = models.CharField(max_length=12)
created_at = models.DateTimeField(default=timezone.now)
last_modified = models.DateTimeField(auto_now=True)
description = models.TextField()
slug = models.SlugField(unique=True)
property_photo = models.ImageField(default='default.jpg', upload_to='hotel_photos')
star_rating = models.PositiveIntegerField()
contact_person = models.ForeignKey(UserProfile, on_delete = models.CASCADE, null=True, blank=True,)
class Room(models.Model):
hotel = models.ForeignKey(Hotels,on_delete = models.CASCADE, null=True, blank=True,)
room_photo = models.ImageField(default='default.jpg', upload_to='room_photos')
room_Name = models.CharField(max_length = 200)
room_details = models.CharField(max_length = 500)
room_Capacity = models.PositiveIntegerField(default = 0)
slug = models.SlugField(unique=True)
# guest_numbers = models.IntegerField(default=0)
room_Price= models.PositiveIntegerField(default = 0)
total_Rooms = models.PositiveIntegerField(default=0)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
user = models.ForeignKey(UserProfile, on_delete=models.CASCADE, null=True, blank=True,)
More details
From the above models, a hotel can have as many rooms as possible. Now i want to fetch the lowest priced room for each hotel. I tried to use Hotels.objects.aggregate(min_price=Min('room__room_Price')) but it is fetching the overall minimum price of all the hotel rooms. Kindly assist
You can try to add ordering to your model and then find lowest price in a loop:
class Room(models.Model):
...
class Meta:
ordering = ['room_Price']
And filter in views:
lowest_prices = {}
for i in Hotels.objects.all():
price = Room.objects.filter(hotel=i.id)[0].room_Price
lowest_prices[i.name] = price
print(lowest_prices)
I put prices in a dict, but you can do anything you want with it.