django admin populate a field while saving model - python

I have a model
class MyModel(models.Model):
title = models.CharField(max_length=100)
net_price = models.IntegerField(default=0)
sold_price = models.IntegerField(default=0)
profit = models.IntegerField(default=0, blank=True, null=True)
Here in admin when I givenet_price and sold_price and save it I want the profit to be calculated automatically. How can I do this ?

You can add a .clean() method in your models to calculate the value of profit if not supplied.
Doing this will set the profit attribute on the instance if the value was not provided from the admin.
class MyModel(models.Model):
title = models.CharField(max_length=100)
net_price = models.IntegerField(default=0)
sold_price = models.IntegerField(default=0)
profit = models.IntegerField(default=0, blank=True, null=True)
def clean(self):
if getattr(self, 'profit', None) is None: # check that current instance has 'profit' attribute not set
self.profit = self.sold_price - self.net_price # calculate and assign 'profit'

Related

Django Models to calculate discount

I am doing an ecommerce project as my first django project and I am having some trouble on how to calculate my actual price and my discount price if there is a discount price so what I want to do is that if the admin adds a discount to a product I want to add a value to a field inside a model called discount price and in that model I want to calculate how much discount percentage the admin has put in and what the discounted price would be after applying the discount percentage
ps: English is my second language and I'm sorry if you were not able to understand
tldr : I want to calculate the price and the discount percentage and make the value it to another field in the model called discount price
this is my models for the product and discounts(please point out how I should improve and any of my mistakes)
from django_extensions.db.fields import AutoSlugField
from django.core.validators import MinValueValidator, MaxValueValidator
# Create your models here.
class Products(models.Model):
product_name = models.CharField(max_length=200, unique=True)
slug = AutoSlugField(populate_from=['product_name'], unique=True)
description = models.TextField(max_length=500, blank=True)
price = models.IntegerField(validators = [MinValueValidator(0)])
discount_price = models.IntegerField(validators = [MinValueValidator(0)],null=True)
image1 = models.ImageField(upload_to='photos/Products')
image2 = models.ImageField(upload_to='photos/Products')
image3 = models.ImageField(upload_to= 'photos/Products')
image4 = models.ImageField(upload_to= 'photos/Products')
stock = models.IntegerField(validators=[MinValueValidator(0)])
Is_available = models.BooleanField(default=True)
category = models.ForeignKey(Category, on_delete=models.CASCADE)
created_date = models.DateTimeField(auto_now_add=True)
modified_date = models.DateTimeField(auto_now=True)
class Meta:
verbose_name = 'products'
verbose_name_plural = 'products'
def get_url(self):
return reverse('product_page',args=[self.category.slug, self.slug])
def __str__(self):
return self.product_name
class OfferProduct(models.Model):
product = models.OneToOneField(Products, related_name='category_offers', on_delete=models.CASCADE)
discount = models.IntegerField(validators=[MinValueValidator(0),MaxValueValidator(99)],null=True,default = 0)
is_active = models.BooleanField(default =True)
class Meta:
verbose_name = 'Offer Product'
verbose_name_plural = 'Offer Products'
def __str__(self):
return self.product.product_name
========= model.py =================
class ProductModel(models.Model):
name= models.CharField(max_length=300)
og_price = models.IntegerField()
discount = models.IntegerField()
discounted_price = models.IntegerField(null=True)
sell_price = models.IntegerField(null=True)
created_at = models.DateField(auto_now_add=True)
#property
def discounted_price(self):
return ((self.og_price)*(self.discount))/100
#property
def sell_price(self):
return (self.og_price)-(self.discounted_price)
=========== Output ============
example : price : 1000, discount: 100 so the percentage is 10%
you can add a property in the OfferProduct:
#property
def discount_percentage(self):
return (100 * discount)/product.price #add your logic

How to access model from Foreign Key, Django?

I have 2 models in my project. What I want to do is access CustomUser model field "user_coins". But the problem is that I need to get it with only having offer_id from the TradeOffer model. So essentially what I would like to happen is to find the TradeOffer field with offer_id and through ForeignKey get the CustomUser field user_coins that the offer_id belongs to. I can't seem to figure out how to do that.
class CustomUser(AbstractUser):
username = models.CharField(max_length=32, blank=True, null=True)
name = models.CharField(max_length=200, unique=True)
user_coins = models.FloatField(default=0.00)
class TradeOffers(models.Model):
name = models.ForeignKey(CustomUser, on_delete=models.SET_NULL, null=True)
offer_id = models.CharField(max_length=150, unique=True)
offer_state = models.IntegerField()
offer_message = models.TextField(null=True)
trade_id = models.CharField(max_length=150, unique=True, null=True)
date_added = models.DateTimeField(auto_now_add=True)
Simple. To get the "user_coins" through "TradeOffers" objects you have to do this:
tradeoffer = TradeOffers.objects.get(offer_id = <whatever>) #Get the object.
user_coins = tradeoffer.name.user_coins #Get the user_coins field.
Or directly:
user_coins = TradeOffers.objects.get(offer_id = <whatever>).name.user_coins

How to structure models in Django?

I'm working on a project using Python(3.7) and Django(3) in which I have to create some models to store reports. There are 4 different models to represents each type of report with only 2 common fields(RequestId, InstCode) but the rest of the fields are different. In the end, I have to display the 10 recent reports (mixed from all models) on the home page.
Here's how I have implemented my models at the moment:
From models.py:
class DistributionReport(models.Model):
RequestId = models.CharField(max_length=256, blank=False, default=0)
InstCode = models.CharField(max_length=3, blank=False)
Currency = models.CharField(max_length=3, blank=False)
Denomination = models.IntegerField(blank=False, default=0)decimal_places=2)
date = models.DateField(default=datetime.date.today)
class Meta:
verbose_name = 'Distribution Report'
def __str__(self):
return self.RequestId
class ExpenditureReport(models.Model):
RequestId = models.CharField(max_length=256, blank=False, default=0)
InstCode = models.CharField(max_length=3, blank=False)
StaffExpenditure = models.DecimalField(max_digits=12, decimal_places=2)
Month = models.IntegerField(blank=False, default=0)
Quarter = models.IntegerField(blank=False, default=0)
Year = models.IntegerField(blank=False, default=0)
class Meta:
verbose_name = 'Expenditure Report'
def __str__(self):
return self.RequestId
class StorageReport(models.Model):
RequestId = models.CharField(max_length=256, blank=False, default=0)
InstCode = models.CharField(max_length=3, blank=False)
Currency = models.CharField(max_length=5, blank=False)
Denomination = models.IntegerField(blank=False, default=0)
date = models.DateField(default=datetime.date.today)
class Meta:
verbose_name = 'Processing Report'
def __str__(self):
return self.RequestId
class AssetsReport(models.Model):
RequestId = models.CharField(max_length=256, blank=False, default=0)
InstCode = models.CharField(max_length=3, blank=False)
AssetClassificationId = models.IntegerField(blank=False, default=0)
VaultCapacity = models.DecimalField(max_digits=10, decimal_places=2)
Year = models.IntegerField(blank=False, default=0)
HalfOftheYear = models.IntegerField(blank=False, default=0)
class Meta:
verbose_name = 'Assets Report'
def __str__(self):
return self.RequestId
What is the best way to structure these models, so I can query the recent reports ( It can be of any type)?
Look into how normalization works, since all models have 2 common fields lets say you have a table called Report and it has the fields RequestId and InstCode. Now all your other models can be said to be in a XYZReport is a Report kind of relationship.
You can achieve this using a OneToOne Field like so:
class Report(models.Model):
RequestId = models.CharField(max_length=256, blank=False, default=0)
InstCode = models.CharField(max_length=3, blank=False)
DISTRIBUTION = 'DI'
EXPENDITURE = 'EX'
STORAGE = 'ST'
ASSETS = 'AS'
REPORT_TYPE_CHOICES = [
(DISTRIBUTION, 'Distribution Report'),
(EXPENDITURE, 'Expenditure Report'),
(STORAGE, 'Storage Report'),
(ASSETS, 'Assets Report'),
]
report_type = models.CharField(
max_length=2,
choices=REPORT_TYPE_CHOICES,
default=DISTRIBUTION,
)
# Any more common fields
class DistributionReport(models.Model):
report = models.OneToOneField(
Report,
on_delete=models.CASCADE,
related_name = 'distribution_report'
)
# Other fields
# Other Report models in similar fashion
Now whenever making an object of any kind of report also make an object of Report and assign it to report attribute of the models. Save both the models, also to figure out what kind of report an instance of Report is add the relevant report_type to the report instance like so:
report.report_type = Report.DISTRIBUTION # In case of Distribution Report
To figure out whether an instance of Report is of a particular type:
report.report_type == report.DISTRIBUTION # will get whether Report is a DistributionReport
To get the specific types object use the related_name set in the OneToOne Field:
distribution_report = report.distribution_report

Django Rest Framework: How to get instance of related foreign key

Note: IF INFORMATION BELOW IS NOT CLEAR TO UNDERSTAND - PLEASE ASK ME, I WILL UPDATE AND POST INFORMATION YOU NEED | It is important for me
In Warehouse(models.Model) I have amount attribute and
in ChosenProduct(models.Model) - quantity
I'm trying to get amount in Warehouse through chosen_products instance in App_formSerializer to add the quantity of chosen_product
But I can not get the chosen_products objects from instance
--> below Out:
class WarehouseSerializer(serializers.ModelSerializer):
category_name = serializers.ReadOnlyField(
source='category_product.category_name')
posted_user = serializers.ReadOnlyField(
source='posted_user.username')
class Meta:
model = Warehouse
fields = ['id', 'category_product', 'category_name', 'condition',
'product_name', 'amount', 'barcode', 'f_price', 'created_at', 'updated_at', 'posted_user']
class ChosenProductSerializer(serializers.ModelSerializer):
product_info = WarehouseSerializer(source='product', read_only=True)
period_info = Product_periodSerializer(source='period', read_only=True)
class Meta:
model = ChosenProduct
exclude = ('app_form',)
class App_formSerializer(serializers.ModelSerializer):
chosen_products = ChosenProductSerializer(many=True)
def update(self, instance, validated_data):
instance.terminated = validated_data.get('terminated', instance.terminated)
if instance.terminated == True :
print('-----------TRUE--------------------')
print(instance.chosen_products)
print('-----------PRINT--------------------')
instance.save()
return instance
class Meta:
model = App_form
fields = '__all__'
Out
-----------TRUE--------------------
creditapi.ChosenProduct.None
-----------PRINT--------------------
QUESTION UPDATED
models.py
class Warehouse(models.Model):
category_product = models.ForeignKey(
Category_product, on_delete=models.CASCADE)
product_name = models.CharField(max_length=200, unique=True)
condition = models.BooleanField(default=False)
amount = models.IntegerField()
barcode = models.BigIntegerField()
f_price = models.CharField(max_length=255, null=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
posted_user = models.ForeignKey(User, on_delete=models.CASCADE, default=1)
def __str__(self):
return self.product_name
class App_form(models.Model):
phone_regex = RegexValidator(regex=r'^\+?1?\d{9,12}$', message="Phone number must be entered in the format: '998981234567'. Up to 12 digits allowed.")
terminated = models.BooleanField(default=False)
name = models.CharField(max_length=150)
phone_number = models.CharField(validators=[phone_regex], max_length=13)
def __str__(self):
return self.surname
class ChosenProduct(models.Model):
product = models.ForeignKey(Warehouse, on_delete=models.CASCADE)
quantity = models.IntegerField()
app_form = models.ForeignKey(App_form, on_delete=models.CASCADE, related_name='chosen_products')
def __str__(self):
return self.product.product_name
If you write instance.chose_products you access the manager, not the QuerySet that contains the items. You can use .all() to obtain the QuerySet with all the objects:
print(instance.chosen_products.all())
If you access a ForeignKey in reverse, you have a manager, since zero, one, or more elements can refer to the instance.
You can for example aggregate over the chose_products, for example if you want to retrieve the number of related chose_products, you can use:
print(instance.chosen_products.count())
I would however advise not store (aggregated) data in the App_form, but aggregate data when you need it. Data duplication is an anti-pattern, and it turns out it is hard to keep data in sync.

Django models, set unqiue attribute for M2M field per object

Currently, have a database with an Item table and a Stock table. There is a many to many relationship between the two. A single item object can have many sizes. The next step is to assign an 'inStock' options to the item per size.
Any thoughts on acheiving this?
Current models.py
class Stock(models.Model):
size = models.CharField(max_length=30)
stock = models.BooleanField(default=False)
def __str__(self):
return self.size
class Item(models.Model):
title = models.CharField(max_length=250, null=True, unique=True)
price = models.DecimalField(max_digits=8, decimal_places=2)
aw_product_id = models.CharField(max_length=11, null=True) # Removed because multiple products has similar identifer
url = models.URLField(max_length=250) # Removed 'unique=True'as the aw_prod_id will throw an integrity error
image = models.CharField(max_length=1000)
retailer = models.CharField(max_length=250)
category = models.CharField(max_length=100)
featured = models.CharField(max_length=10, default='NO')
gender = models.CharField(max_length=100, null=True)
sizes = models.ManyToManyField(Stock)
uniq_id = models.CharField(max_length=11, null=True, unique=True) # Removed because multiple products has similar identifer
def __str__(self):
return self.title
You can use the through argument to ManyToManyField to specify another model to use for the relationship, with additional fields instead of the autogenerated model that django creates by default.

Categories