I have a model like
class tbl_payment(models.Model):
document_id = models.ForeignKey(tbl_invoice, on_delete=models.CASCADE)
client_id = models.ForeignKey(tbl_customer, on_delete=models.CASCADE)
total_amount = models.DecimalField(max_digits=8, decimal_places=2, blank=True, null=True)
paid_amount = models.DecimalField(max_digits=8, decimal_places=2, blank=True, null=True)
balance = models.DecimalField(max_digits=8, decimal_places=2, blank=True, null=True)
date = models.DateField(blank=True, null=True)
status = models.CharField(max_length=50)
Now what I want to do is whenever a new record is added, or an existing record changes, balance should be updated as the difference between total_amount and paid_amount (simple maths), and based on my balance column, I want to save status as Paid, Partial or Unpaid.
I want to refrain from calculating the balance in my views and then saving in the database, instead, I want to handover this part to my models so that my models take care of the balance and I may avoid errors which I am subconsciously afraid of.
I came to know that this is done something like this
class tbl_payment(models.Model):
document_id = models.ForeignKey(tbl_invoice, on_delete=models.CASCADE)
client_id = models.ForeignKey(tbl_customer, on_delete=models.CASCADE)
total_amount = models.DecimalField(max_digits=8, decimal_places=2, blank=True, null=True)
paid_amount = models.DecimalField(max_digits=8, decimal_places=2, blank=True, null=True)
balance = models.DecimalField(max_digits=8, decimal_places=2, blank=True, null=True)
date = models.DateField(blank=True, null=True)
status = models.CharField(max_length=50)
#property
def balance(self, value):
return self.total_amount - paid_amount
but what should I pass in place of value??
also when I try to get due_amount value like this
tbl_bulk_payment.objects.get(pk=1).due_amount
it gives due_amount() missing 1 required positional argument: 'value'
what is the correct way of doing this??
you have to override save() function
class tbl_payment(models.Model):
class Status(models.TextChoices):
paid = 'Paid', 'Paid'
partial = 'Partial', 'Partial'
unpaid = 'Unpaid', 'Unpaid'
document_id = models.ForeignKey(tbl_invoice, on_delete=models.CASCADE)
client_id = models.ForeignKey(tbl_customer, on_delete=models.CASCADE)
total_amount = models.DecimalField(max_digits=8, decimal_places=2, blank=True, null=True)
paid_amount = models.DecimalField(max_digits=8, decimal_places=2, blank=True, null=True)
balance = models.DecimalField(max_digits=8, decimal_places=2, blank=True, null=True)
date = models.DateField(blank=True, null=True)
status = models.CharField(max_length=50, choices=Status.choices)
def save(self, *args, **kwargs):
self.balance = self.total_amount - self.paid_amount
if self.balance >= 0:
self.status = Status.paid
elif self.paid_amount == 0:
self.status = Status.unpaid
else:
self.status = Status.partial
super(tbl_payment, self).save(*args, **kwargs)
P.S. your Model class name is not following the python or Django naming rule.
class Payment(models.Model) would be better.
and document_id => document. because
payment = Payment.objects.get(pk=1)
payment.document_id
in this case payment.document_id would return document instance. (not document instance's id). so document is more Django like style than document_id
Related
I'm building a cart model with the following code.
from django.db import models
class Item(models.Model):
name = models.CharField(max_length=200)
price = models.DecimalField(max_digits=8, decimal_places=2)
def __str__(self):
return self.name
class Order(models.Model):
date = models.DateTimeField(auto_now_add=True)
transcation_id = models.CharField(max_length=200, null=True)
def __str__(self):
return str(self.date)
class OrderItem(models.Model):
item = models.ForeignKey(Item, on_delete=models.CASCADE, blank=True, null=True)
order = models.ForeignKey(Order, on_delete=models.CASCADE, blank=True, null=True)
quantity = models.IntegerField(default=0, blank=True, null=True)
The many-to-one relationship between Item and Order allows one Order to contain many Item and that looks fine.
A model instance can be simply cloned as already answer in this question.
My problem is, if the price of an Item is changed. The price of contained items in Order is change too. But I don't want it to be changed. In the situation that customer already make a purchase, the price cannot be change. Is there anyway to clone the Order instance that completely not related to the other model?
Save the price manually
class OrderItem(models.Model):
item = models.ForeignKey(Item, on_delete=models.CASCADE, blank=True, null=True)
order = models.ForeignKey(Order, on_delete=models.CASCADE, blank=True, null=True)
quantity = models.IntegerField(default=0, blank=True, null=True)
price = price = models.DecimalField(max_digits=8, decimal_places=2, null=True, default=None)
def save():
if self.pk == None:
self.price = self.item.price
super(OrderItem, self).save()
I'm trying to perform a Sum of different rows of the Model according to specific column values, in this case I want to Sum according to a week and an specific car, so for example:
Car Week Payment
1 2020-W06 $500
1 2020-W06 $300
2 2020-W06 $200
1 2020-W05 $500
So I pass to the query the car & the week and it should get the sum of the payments according to those values
I pass Car = 1 and Week = 2020-W06 and Payment Sum = $800
this is my queryset:
payed = Pagos.objects.filter(carro_id=1, semana=semana).annotate(total=Sum('pago'))
and thi is the result I'm getting:
<Pagos: Pagos object (6)>, <Pagos: Pagos object (12)>]
I don't understand why I don't get the Sum
models.py
class Pagos(models.Model):
carro = models.ForeignKey(
Carros, on_delete=models.CASCADE, blank=False, null=False)
pago = models.DecimalField(max_digits=6, decimal_places=2)
fecha = models.DateField(
auto_now=False, auto_now_add=False, blank=True, null=True)
semana = models.CharField(max_length=20)
startweek = models.DateField(
auto_now=False, auto_now_add=False, blank=True, null=True)
endweek = models.DateField(
auto_now=False, auto_now_add=False, blank=True, null=True)
renta = models.ForeignKey(
Renta, on_delete=models.PROTECT, blank=False, null=False)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class Meta:
verbose_name_plural = "Pagos"
def get_absolute_url(self):
return reverse('pagos')
class Carros(models.Model):
nombre = models.CharField(max_length=20, blank=True, null=True)
marca = models.CharField(max_length=25)
modelo = models.CharField(max_length=25)
year = models.IntegerField()
placa = models.CharField(max_length=10, unique=True)
color = models.CharField(max_length=10)
conductor = models.ForeignKey(
Conductores, on_delete=models.SET_NULL, blank=True, null=True)
propietario = models.ForeignKey(Propietarios, on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class Meta:
verbose_name_plural = "VehĂculos"
def __str__(self):
return self.nombre
def get_absolute_url(self):
return reverse('carros')
According to docs annotate adds a filed to each of your model objects, so you get the sum and you can access it like this(docs):
payed[0].total
If you want to get total value not on just one field you need to use aggregate(docs), it will be like:
payed = Pagos.objects.filter(carro_id=1, semana=semana).aggregate(total=Sum('pago'))
print(payed)
# {total: x}
About the part you said you want different sums based on fields you can use conditional clauses in your annotate and aggregation.
suppose I have two models
class Project(models.Model):
project_number = models.Charfield(primary_key=True, max_length=10)
project_title = models.Charfield(max_length=100)
total_cost = models.DecimalField(max_digits=8, decimal_places=2, blank=True, null=True)
class FieldChangeOrder(models.Model):
project = models.FoereignKey('Project', on_delete=models.CASCADE)
funding_amount = models.DecimalField(max_digits=8, decimal_places=2, blank=True, null=True)
percentage_of_funding = ( funding_amount / total_cost ) * 100
How do I access total_cost from Project model from FieldChangeOrder model to calculate the value for percentage_of_fundiing field?
I tried the following
class Project(models.Model):
project_number = models.Charfield(primary_key=True, max_length=10)
project_title = models.Charfield(max_length=100)
total_cost = models.DecimalField(max_digits=8, decimal_places=2, blank=True, null=True)
class FieldChangeOrder(models.Model):
project = models.FoereignKey('Project', on_delete=models.CASCADE)
funding_amount = models.DecimalField(max_digits=8, decimal_places=2, blank=True, null=True)
#property
def percentage_of_funding(self):
return (self.funding_amount/self.project.total_cost) * 100
but ^ errors out saying FieldChangeOrder object has no attribute 'project'
I am fairly new to django and am just dabbling right now. Any help is much appreciated
Simply, you can use dot notation to access fields on foreign keys:
as an example:
field_changer_order = FieldChangeOrder.objects.get(pk=1)
project_total_cost = field_changer_order.project.total_cost
I would like to create a model which must be accepted by the moderator before adding, after which every change in eg the title in this model must also be accepted
class MangaRequest(models.Model):
title = models.CharField(max_length=191)
type = models.CharField(max_length=30,choices=TYPE, blank=True, default='', null=True)
status= models.CharField(max_length=30, choices=STATUS, blank=True, default='', null=True)
date_start = models.DateField(blank=True, null=True)
data_end = models.DateField(blank=True, null=True)
age_restrictions = models.ForeignKey(OgraniczenieWiekowe, null=True, default='', blank=True)
volumes = models.SmallIntegerField(null=True, blank=True, default=0)
chapter = models.SmallIntegerField(null=True, blank=True, default=0)
is_accept = models.BooleanField(default=False)
delete = models.BooleanField(default=False)
This is my model and I have done it
def save(self, *args, **kwargs):
if self.is_accept == True:
manga = MangaAccept.objects.create(...)
super(MangaRequest, self).delete()
return manga
else:
super(MangaRequest, self).save()
And the same way is about deletes
My question is how can ACCEPT or REJECT for model and everyone field in this model ? Any sugestion ?
I'm converting an ancient Client/Server app (Delphi) into a Django app for a small brick and morter bookstore (wife's).
Since most functions are admin, I'm using the Django admin interface with grappelli for some easier lookups.
I have 3 models: Book, Sale and Item.
class Book(TimeStampedModel):
"""
Books are described individually and are related to collections
as many to many. Every book in this system is unique - i.e. there are
not quantity fields. This is optimized for used book stores where book
condition is essential.
"""
STATUS_CHOICES = (
('IN STOCK', 'IN STOCK'),
('SOLD', 'SOLD'),
('ON LOAN', 'ON LOAN'),
('HOLD', 'HOLD'),
)
isbn = models.CharField(max_length=50, blank=True)
title = models.CharField(max_length=200, blank=False, db_index=True)
author = models.CharField(max_length=200, blank=True)
sell_price = models.DecimalField(max_digits=10,decimal_places=2, default=0)
description = models.TextField()
collections = models.ManyToManyField(Collection)
class Meta:
index_together = [
["author", "title"],
["status", "title", "author"],
]
def __unicode__(self):
return "%s [%d] - %s - $%.2f" % (self.title, self.id, self.book_type, self.sell_price)
#staticmethod
def autocomplete_queryset():
instock = Book.objects.filter(status="IN STOCK")
return instock
#staticmethod
def autocomplete_search_fields():
return("id__iexact", "title__istartswith",)
class Sale(TimeStampedModel):
"""
Sales group all sold items which may or may not be books and are sold to contacts.
We use a "generic" contact of "cash" for non named contacts
"""
PAYMENT_TYPE_CHOICES = ( ('Cash', 'Cash'), ('Charge', 'Charge'), ('Check', 'Check'))
contact = models.ForeignKey(Contact, null=True)
sale_date = models.DateField(blank=True,default=datetime.date.today, db_index=True)
payment_type = models.CharField(max_length=50, choices=PAYMENT_TYPE_CHOICES)
taxed = models.BooleanField(default=True)
tax_exempt_no = models.CharField(blank=True, max_length=50)
sales_tax = models.DecimalField(max_digits=10, decimal_places=2, blank=True, null=True)
amt_tender = models.DecimalField(max_digits=10, decimal_places=2, blank=True, null=True)
pct_discount = models.SmallIntegerField(blank=True, null=True)
amt_credit = models.DecimalField(max_digits=10, decimal_places=2, blank=True, null=True)
amt_shipping = models.DecimalField(max_digits=10, decimal_places=2, blank=True, null=True)
amt_due = models.DecimalField(max_digits=10, decimal_places=2, blank=True, null=True)
tot_sale = models.DecimalField(max_digits=10, decimal_places=2, blank=True, null=True)
tot_items = models.DecimalField(max_digits=10, decimal_places=2, blank=True, null=True)
ordering = ['-sale_date']
def __unicode__(self):
return str(self.sale_date)
class Item(TimeStampedModel):
"""
Items are usually books sold on a sale. Items can also be entered manually
at time of sale if they are not books from inventory
"""
sale = models.ForeignKey(Sale)
book = models.ForeignKey(Book, null=True, blank=True)
item_desc = models.CharField(blank=True, max_length=200)
cost = models.DecimalField(max_digits=10, decimal_places=2, blank=True, null=True)
sell_price = models.DecimalField(max_digits=10, decimal_places=2, blank=True, null=True)
def __unicode__(self):
return self.item_desc
For the Sale form, I use an admin form with Tabular inline Items. Items are usually books (via a foreign key lookup), but can also be entered manually for non-inventory items so I have a sell_price both in the book model and in the item model.
class ItemInline(admin.TabularInline):
model = Item
raw_id_fields = ("book",)
autocomplete_lookup_fields = {
'fk': ['book'],
}
extra = 2
What I'd like to do in the foreign key lookup is to return the key of the book AND fill in the Item's sellprice with the sellprice from the book I looked up.
I have the basic lookup working just fine but can't find out how to set the item sellprice to the book's sellprice immediately after the lookup.
Any advice is appreciated! I have tried figuring out the objects to put some JS logic in but the inlines get object ids created dynamically, I think. I'm no JS expert.