Child model updates Parent model in Django ForeignKey relationship - django

Assuming the following models schema,
Parent model:
class Batch(models.Model):
start = models.DateTimeField()
end = models.DateTimeField()
One of many child models:
class Data(models.Model):
batch = models.ForeignKey(Batch, on_delete=models.ON_CASCADE)
timestamp = models.DateTimeField()
My goals is the following: to have a start field of parent model that is always updated when any child model is modified.
Basically, if the timestamp of a newly data instance is older than the start field I want the start field to be updated to that instance timestamp value. In the case of deletion of the data instance which is the oldest time reference point I want batch start field to be updated to the second oldest. Vice-versa for the end field.

One of the possible way to do this is to add post or pre-save signal of relative models and Update your necessary fields according to this. Django official documentation for signal, link. I want to add another link, one of the best blog post i have seen regarding django signal.
Edit for André Guerra response
One of easiest way to do a get call and bring Batch instance. What i want to say
#receiver(post_save,sender=Data)
def on_batch_child_saving(sender,instance,**kwargs):
batch_instance = Batch.objects.get(pk=instance.batch)
if (instance.timestamp < batch_instance.start):
batch_instance.start = instance.timestamp
batch_instance.save()
elif (instance.timestamp > batch_instance.end):
batch_instance.end = instance.timestamp
batch_instance.save()

Based on Shakil suggestion, I come up with this: (my doubt here was on how to save the parent model)
#receiver(post_save,sender=Data)
def on_batch_child_saving(sender,instance,**kwargs):
if (instance.timestamp < instance.batch.start):
instance.batch.start = instance.timestamp
instance.batch.save()
elif (instance.timestamp > instance.batch.end):
instance.batch.end = instance.timestamp
instance.batch.save()

Related

Populating django model with objects from other model

I'm new to django, but working on an app for a volunteer sailing organization in my local area. Not sure how to ask this question since it's fairly general but I want the following to happen based on two models;
Yacht class (boat name, skipper, color, etc.)
Race_Event class (event date, time results for each boat)
Step 1: The user will need to create a Race_Event each week. I want the boats from the Yacht model to be loaded into the Race_Event.
Step 2: The user will enter race times for each boat.
Is there a way to pre-load objects from one model into another? With a ForeignKey the user has to add the boats each time. Any direction for me to research would be helpful.
Here is the simplified code so far;
class Yacht (models.Model):
yacht_classes = [('A', 'A'),('A1', 'A1'),]
yacht_type = [('J-29','J-29'),('J-24','J-24'),]
yacht_name = models.CharField(max_length=75)
yacht_type = models.CharField(max_length=25, choices=yacht_type,
default='J-29')
yacht_class = models.CharField(max_length=25, choices=yacht_classes)
skipper = models.ForeignKey(Skipper, on_delete=models.CASCADE)
def __str__(self):
return self.yacht_name
class Event (models.Model):
race_date = models.DateTimeField(default=timezone.now)
#yachts = #how to Include Yacht.objects.all() to the field?
class Results (models.Model):
pass
Thanks
Yes, u can use signals...
after objects is saved u can call post_save and add all yachts to race
more => https://docs.djangoproject.com/en/3.1/ref/signals/#post-save
but i dont think this is good way...
(not every time all the data must be present or must be saved => this save rows in database)
i recomment you to use m2M between race and ship with throught table where time is saved in table between.
then its on you how you present this problem to end-user.
with this solution you save only data which are needed.
this can be done with
https://docs.djangoproject.com/en/3.1/topics/db/models/#extra-fields-on-many-to-many-relationships

Django: How to update model instance using pre_save signal?

I want to create a pre_save signal which will update the already created model instance whenever the sender is updated. I want to do it in pre_save signal.
The previous instance of the model will be either deleted and new instance will be created or the previous instance will be updated.
My models:
class Sales(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE,null=True,blank=True)
company = models.ForeignKey(Company,on_delete=models.CASCADE,null=True,blank=True)
party_ac = models.ForeignKey(Ledger1,on_delete=models.CASCADE,related_name='partyledgersales')
sales = models.ForeignKey(Ledger1,on_delete=models.CASCADE,related_name='saleledger')
date = models.DateField(default=datetime.date.today,blank=False, null=True)
sub_total = models.DecimalField(max_digits=10,decimal_places=2,blank=True, null=True)
class Journal(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE,null=True,blank=True)
company = models.ForeignKey(Company,on_delete=models.CASCADE,null=True,blank=True,related_name='Companyname')
voucher_id = models.PositiveIntegerField(blank=True,null=True)
date = models.DateField(default=datetime.date.today)
by = models.ForeignKey(Ledger1,on_delete=models.CASCADE,related_name='Debitledgers')
to = models.ForeignKey(Ledger1,on_delete=models.CASCADE,related_name='Creditledgers')
debit = models.DecimalField(max_digits=10,decimal_places=2,null=True)
credit = models.DecimalField(max_digits=10,decimal_places=2,null=True)
My pre_save signal for creation:
#receiver(pre_save, sender=Sales)
def user_created_sales(sender,instance,*args,**kwargs):
if instance.sub_total != None:
Journal.objects.update_or_create(user=instance.user,company=instance.company,date=instance.date,voucher_id=instance.id,by=instance.party_ac,to=instance.sales,debit=instance.sub_total,credit=instance.sub_total)
I want to create a signal which will update the Journal instance whenever the sender model or Sales model is update or delete the previous instance or create a new one.
Any idea how to perform this?
Thank you
update_or_create takes two sets of parameters:
kwargs which are the parameters that uniquely identify the row to fetch from the database
defaults which are the parameters that should be updated
Right now, you're never updating existing instances of Journal because you haven't specified defaults. You're looking for instances that match all the parameters, if one exists, you don't do anything, if it doesn't exist you create it.
You haven't told us what makes a Journal entry unique (and how it relates to a Sales entry, since there is no ForeignKey relationship between them).
update: with voucher_id, you now have way of looking up the corresponding Journal entries.
But something like:
Journal.objects.update_or_create(
user=instance.user,
company=instance.company,
voucher_id=instance.id,
defaults={
'date': instance.date,
'debit': instance.sub_total,
'credit': instance.sub_total,
'by': instance.party_ac,
'to': instance.sales}
)
would look for existing instance with the same user, company, and voucher_id and update the date, by, to, credit and debit.

Model integrity check in django

I have a model named Entry that has the following fields
from django.contrib.auth.models import User
class Entry(models.Model):
start = models.DateTimeField()
end = models.DateTimeField()
creator = models.ForeignKey(User)
canceled = models.BooleanField(default=False)
When I create a new entry I don't want to be created if the creator has allready a created event between the same start and end dates. So my Idea was when user posts data from the creation form
if request.method == 'POST':
entryform = EntryAddForm(request.POST)
if entryform.is_valid():
entry = entryform.save(commit=False)
entry.creator = request.user
#check if an entry exists between start and end
if Entry.objects.get(creator=entry.creator, start__gte=entry.start, end__lte=entry.end, canceled=False):
#Add to message framework that an entry allready exists there
return redirect_to('create_form_page')
else:
#go on with creating the entry
I was thinking that maybe a unique field and checking properly db integrity would be better but it is the canceled field that's troubling me in to how to choose the unique fields. Do you think there will be somethng wrong with my method? I mean does it make sure that no entry will be set between start and end date for user is he has allready saved one? Do you think its better this code do go to the pre-save? The db will start empty, so after entering one entry, everything will take its course (assuming that...not really sure...)
You need to use Q for complex queries.
from django.db.models import Q
_exists = Entry.objects.filter(Q(
Q(Q(start__gte=entry.start) & Q(start__lte=entry.end)) |
Q(Q(end__gte=entry.start) & Q(end__lte=entry.end)) |
Q(Q(start__lte=enrty.start) & Q(end__gte=entry.end))
))
if _exists:
"There is existing event"
else:
"You can create the event"
Since I do not test this, I use Q objects whereever I thought would be necessary.
Using this query, you will not need any unique check.

django post_save signal sends outdated inline formsets

Consider the following:
class OrderForm(models.Model):
title = models.CharField(max_length=100)
desc = models.TextField()
class OrderFormLine(models.Model):
order = models.ForeignKey(OrderForm)
lagel = models.CharField(max_length=100)
qty = models.IntegerField(...)
price = models.FloatField(...)
Now I want to send an email with the orderform details whenever someone creates one or modify one.
No rocket science so far .. let's just use a post_save signal;
post_save.connect(email_notify, sender=OrderForm)
But there's one tiny problem, the OrderForm object passed to email_notify is updated with the new data as expected, but not the related OrderFormLine items.
I've tried to override the save methods in the admin AND in the model, I've tried to save the object, the form and its relation before passing it to my notification handler, nothing works.
I'm aware that I could attach the post_save signal to the OrderItem model, but then the email would be sent for each items.
Help I'm on the brink of madness.
UPDATE:
Found a simple and reliable solution
Short story:
def email_notify_orderform(sender, **kwargs):
instance = kwargs['instance']
ct = ContentType.objects.get_for_model(OrderForm)
if ct.id == instance.content_type.id:
print instance.is_addition()
print instance.is_change()
print instance.is_deletion()
print instance.change_message
print instance.action_time
print instance.get_edited_object().total() # BINGO !
post_save.connect(email_notify_orderform, sender=LogEntry)
The basic problem is that when the main objects post_save signal is sent, the inlines have not been saved yet: the parent model always gets saved first. So, it's not that it's sending old data; in fact it's the current state of the data.
The simplest solution is to create a custom signal and have that signal sent at a place where the inlines have been saved. The save_formset method on ModelAdmin is your hook.

Django, updating from model

assume I have this little model:
class Deal(models.Model):
purchases = models.IntegerField(default=0)#amount of purchases so far
increase_purchases(self,to_add):
self.update( purchases =self.purchases + to_add)
when I try to use this increase_purchases model from shell:
>>> x = Deal.objects.get(id=1)
>>> x.increase_purchases(4)
AttributeError: 'Deal' object has no attribute 'update'
How can I write a proper function to the model so that I can update the selected querys purchases as I want ?
Based on your example and description, you probably want something like this:
class Deal(models.Model):
purchase_count = models.IntegerField(default=0)
def purchase(self, quantity=1):
self.purchase_count = self.purchase_count + quantity
I agree with Ignacio; modify the object and then save it. So in the shell:
> great_deal = Deal.objects.get(id=1)
> great_deal.purchase(4)
> great_deal.save()
> # or w/o an explicite argument it will record a single purchase
> # great_deal.purchase()
Yes, I renamed things a little bit in the Deal model. It just seemed more descriptive this way.
Modify the appropriate fields then call save() on the instance.
Or use the += expression for cleaner code:
class Deal(models.Model):
purchase_count = models.IntegerField(default=0)
def purchase(self, quantity=1):
self.purchase_count += quantity
In Django 1.6.2. Encountered this behavior and used a "filter" then update works as expected. For example, Students.objects.select_for_update().filter(id=3).update(score = 10)
Just fyi: Unless you are handling transactions, modifying each field separately using save() might create data inconsistency in a multi-threaded environment. By the time threadA calls save() on a model, another threadB could have changed the model fields and saved. In which case threadA has to read the updated model and change.