I have Book model. It has some fields like title, year publish and etc. Also, i have overrided save() method. When new books is adding, it checks if book exsists, it creating new directory with name self.title in MEDIA_ROOT path.
def save(self, *args, **kwargs):
book_dir = os.path.join(MEDIA_ROOT, self.title)
# check that at least one file is loading
if all([self.pdf, self.fb2, self.epub]):
raise ValidationError("At least 1 file should be uploaded!")
# create book's directory if it not exists
if os.path.exists(book_dir):
raise ValidationError("This book is already exists!")
else:
os.mkdir(book_dir)
# rename and edit storage location of books to book_dir
for field in [self.image, self.pdf, self.fb2, self.epub]:
field.storage.location = book_dir
super().save(*args, **kwargs) # Call the "real" save() method.
Also i have overrided delete() method, that just remove directory of deleted book.
def delete(self, *args, **kwargs):
book_dir = os.path.join(MEDIA_ROOT, self.title)
rmtree(book_dir)
super().delete(*args, **kwargs) # Call the "real" delete() method.
delete() method works well if i delete only 1 book.
But, if i want to delete multiple files (all actions take place in the admin panel), only DB records got deleted.
So, i want to just catch this moment to remove directory of deleted book.
Looks like pre_delete signal could be useful here: https://docs.djangoproject.com/en/2.2/ref/signals/#pre-delete
Related
I'd like to make sure that nobody can't create an Invitation object with an email that is already in a database either as Invitation.email or as User.email.
To disallow creating Invitation with existing Invitation.email is easy:
class Invitation(..):
email = ...unique=True)
Is it also possible to check for the email in User table? I want to do this on a database or model level instead of checking it in serializer, forms etc..
I was thinking about UniqueConstraint but I don't know how to make the User.objects.filter(email=email).exists() lookup there.
You can override the save() method on the model, and check first in the users table. You should look that is a new model. Something like this I think:
class Invitation(..):
email = ...unique=True)
def save(self, *args, **kwargs):
if self.id is None and User.objects.filter(email=self.email).exists():
raise ValidationError('Email already used.')
else:
super().save(*args, **kwargs)
You can do it in the model.. as below. Or you can do it in the database with a Check Constraint (assuming postgres).. but you still can't avoid adding code to your view, because you'll need to catch the exception and display a message to the user.
class Invitation(models.Model):
def save(self, *args, **kwargs):
if (not self.pk) and User.objects.filter(email=self.email).exists():
raise ValueError('Cannot create invitation for existing user %s.' % self.email)
return super().save(*args, **kwargs)
PS: Some may ask why it is that I am passing *args and **kwargs to the superclass, or returning the return value.. when save has no return value. The reason for this is that I never assume that the arguments or return value for a method I am overriding won't change in the future. Passing them all through if you have no reason to intercept them, is just a good practice.
How about overriding the save method?
class Invitation(...):
...
def save(self, *args, **kwargs):
# check if an invitation email on the user table:
if User.objects.get(id=<the-id>).email:
# raise integrity error:
...
# otherwise save as normal:
else:
super().save(*args, **kwargs)
How do we enforce Django Admin to correctly call .update() instead of .save() to avoid triggering checks meant for object creation?
This is the models.py:
class BinaryChoice():
# field definitions
...
def save(self, *args, **kwargs):
# check if binary
if self.question.qtype == 2:
if self.question.choices.count() < 2:
super(BinaryChoice, self).save(*args, **kwargs)
else:
raise Exception("Binary question type can contain at most two choices.")
else:
super(BinaryChoice, self).save(*args, **kwargs)
This passes the test, no surprises:
class SurveyTest(TestCase):
def test_binary_choice_create(self):
q1 = Question.objects.create(survey=survey, title='Have you got any internship experience?', qtype=Question.BINARY)
BinaryChoice.objects.create(question=q1, choice="Yes")
BinaryChoice.objects.create(question=q1, choice="No")
with self.assertRaises(Exception):
BinaryChoice.objects.create(question=q1, choice="Unsure / Rather not say")
The .save() correctly checks that there isn't already 2 binary choices related to the same Question. However, in Django Admin, when using the interface to update the value (anything arbitrary, for example changing the value from "Yes" to "Sure") and saving it, one would expect the .update() method to be called.
It turns out, according to Django docs and also a relevant thread here, the .save() method is called instead. So now our update operation would fail when there's already 2 BinaryChoice, even if you intend to update a value in-place using the Django Admin's default interface.
For completeness sake, this is admin.py:
#admin.register(BinaryChoice)
class BinaryChoiceAdmin(admin.ModelAdmin):
pass
Instead of trying to patch the ModelAdmin why don't you simply fix your save method? Simply check if the object already has a pk or not before saving:
class BinaryChoice():
# field definitions
...
def save(self, *args, **kwargs):
# check if binary
# Here ↓
if not self.pk and self.question.qtype == 2:
if self.question.choices.count() < 2:
super(BinaryChoice, self).save(*args, **kwargs)
else:
raise Exception("Binary question type can contain at most two choices.")
else:
super(BinaryChoice, self).save(*args, **kwargs)
I am trying to add logging to my Django app using EventLog. I followed an example online but not sure how to pass in the user that makes the changes. The example shows it as user=self.user. Obviously this wouldn't work in my case as it doesn't refer to anything in my model
models.py
class Client(models.Model):
name = models.CharField(max_length=50)
....
def save(self, *args, **kwargs):
# Initial Save
if not self.pk:
log(user=self.user, action='ADD_CLIENT',
extra={'id': self.id})
else:
log(user=self.user, action='UPDATED_CLIENT',
extra={'id': self.id})
super(Client, self).save(*args, **kwargs)
The save method will only know what has been passed into it, this will normally not include the request which is where you would get the current user (request.user).
You should instead add logging in the view which is calling the save method.
user = request.user
I have the following code in one of my models
class PostImage(models.Model):
post = models.ForeignKey(Post, related_name="images")
# #### figure out a way to have image folders per user...
image = models.ImageField(upload_to='images')
image_infowindow = models.ImageField(upload_to='images')
image_thumb = models.ImageField(upload_to='images')
image_web = models.ImageField(upload_to='images')
description = models.CharField(max_length=100)
order = models.IntegerField(null=True)
IMAGE_SIZES = {
'image_infowindow':(70,70),
'image_thumb':(100,100),
'image_web':(640,480),
}
def delete(self, *args, **kwargs):
# delete files..
self.image.delete(save=False)
self.image_thumb.delete(save=False)
self.image_web.delete(save=False)
self.image_infowindow.delete(save=False)
super(PostImage, self).delete(*args, **kwargs)
I am trying to delete the files when the delete() method is called on PostImage. However, the files are not being removed.
As you can see, I am overriding the delete() method, and deleting each ImageField. For some reason however, the files are not being removed.
You can delete a model instance with multiple methods.
One method is by calling delete():
PostImage.objects.get(...).delete()
In this case the delete() is called, hence the files will be removed. However you can also delete objects by using querysets:
PostImage.objects.filter(...).delete()
The difference is that using the latter method, Django will delete the objects in bulk by using SQL DELETE command, hence the delete() method for each object is not called. So probably you are deleting objects using querysets, and therefore files are not being removed.
You can solve this by using post_delete Django signal as follows:
#receiver(post_delete, sender=PostImage)
def post_delete_user(sender, instance, *args, **kwargs):
instance.image.delete(save=False)
instance.image_thumb.delete(save=False)
instance.image_web.delete(save=False)
instance.image_infowindow.delete(save=False)
Please note that if you use this method, you don't have to overwrite the delete() method anymore.
More about this here and here
I have this model:
class UserProfile(models.Model):
"""(UserProfile description)"""
user = models.ForeignKey(User)
image = models.ImageField(upload_to=upload_to)
def save(self):
os.unlink(self.image.path)
super(UserProfile, self).save()
The script fails on the unlink() method, and says the file cannot be found. Any ideas?
The error says
(2, 'No such file or directory')
You need more specific debugging. Furthermore, the way you've written the code probably ensures errors will happen. I'm not entirely sure, but I wouldn't be surprised if the value for UserProfile.image isn't set before a UserProfile record is created.
So I would rewrite your code thusly:
class UserProfile(models.Model):
user = models.ForeignKey(User)
image = models.ImageField(upload_to=upload_to)
def save(self):
if self.image.path:
try:
os.unlink(self.image.path)
except Exception, inst:
raise Exception("Unable to delete %s. Error: %s" % (self.image.path, inst))
super(UserProfile, self).save()
Update
Now that I think about it, it does make sense that once you're calling save, the new information is already in self.image. If your goal is to delete the old images when saving the new, you are going to need to use the pre_save signal in Django to retrieve the old record (and thus the old image path) prior to saving the image. In theory, you could put this in the save method of the UserProfile model, but since it's intended as a side action that does not directly affect the current record, I'd keep it separate.
Here's a sample implementation:
from django.db.models import signals
def delete_old_image(sender, instance, using=None):
try:
old_record = sender.objects.get(pk=instance.pk)
os.unlink(old_record.image.path)
except sender.DoesNotExist:
pass
signals.pre_save.connect(delete_old_image, sender=UserProfile)
You would put this in your models.py file.