Slug field creation using ID - django

this the postmodel and trying to create a slug unique field
The Slug field creation error str(self.id) is returning "none" where as the seld.title is working correctly
class postmodel(models.Model):
title = models.CharField(max_length=100)
author = models.ForeignKey(User,default=None)
body = models.TextField()
slug = models.SlugField(unique=True)
subject=models.TextField()
timestamp = models.DateTimeField(auto_now_add=True,auto_now=False)
updated = models.DateTimeField(auto_now=True,auto_now_add=False)
#models.permalink
def get_absolute_url(self):
return ('blog_post_dpetail', (),
{
'slug': self.slug,
})
def save(self, *args, **kwargs):
if not self.slug:
self.slug = slugify(self.title)+"-"+str(self.id)
super(postmodel, self).save(*args, **kwargs)

You cannot get self.id until the object is saved in your database. So you can modify your save() method to:
def save(self, *args, **kwargs):
super(postmodel, self).save(*args, **kwargs) # Save your model in order to get the id
# Then implement the logic for the slug
if not self.slug:
self.slug = slugify(self.title) + "-" + str(self.id)
self.save() # Call save() again in order to save the slug

self.id is auto increment field in the database, it is exist only after you save object in the database, so you have created a stalemate:
You can save object if slug is none and you can create slug while you not save the object.
hope it help you.

so I came up with a good enough solution based on previous answers.
In models.py
this allows slugfield to be blank when you first submit the form...
slug = models.SlugField(unique=True, allow_unicode=True, blank=True)
override the save function to 1. create the post 2. set the slug of the instance of the post to title + id of the post.
def save(self, *args, **kwargs):
super(Post, self).save(*args, **kwargs)
self.slug = slugify(self.title) + "-" + str(self.id)
return super(Post, self).save(*args, **kwargs)
Hope it helps.

Related

Django-taggit: how to save after adding tags in save()

def save(self, *args, **kwargs):
super(Flow, self).save(*args, **kwargs)
if self.pk:
self.tags.add(str(date.today().year))
self.save()
This doesn't work in my model Flow. How can I save the newly added tag into tags of my flow instance successfully? Thanks.
My Flow model:
class Flow(models.Model):
remarks = models.TextField(null=True, blank=True)
tags = TaggableManager(blank=True)

override the save method in django

I want the create field to change when the price field changes, that is, if I change the rest of the fields, the create field will not change and will remain the same.
class Product(models.Model):
name = models.CharField(max_length=200)
price = models.IntegerField()
create = models.DateTimeField(auto_now=True)
discount = models.IntegerField()
information = models.TextField()
You can try a signal like this:
from django.dispatch import receiver
from django.db.models.signals import pre_save
from django.utils import timezone
#receiver(pre_save, sender=Product)
def update_create_field(sender, instance, **kwargs):
try:
_pre_save_instance = Product.objects.get(pk=instance.pk)
if _pre_save_instance.price != instance.price:
instance.create = timezone.now() #here you can specify any value you want
except Exception as e:
print('Error updating create field!')
print(e)
You can save your initial values in __init__, and check them before save:
class Product(models.Model):
name = models.CharField(max_length=200)
price = models.IntegerField()
# Remove auto_now=True, it does not allow to set values by yourself.
create = models.DateTimeField()
discount = models.IntegerField()
information = models.TextField()
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.create = self.create or datetime.now(UTC)
self._initial_price = self.price
def save(self, *args, **kwargs):
if self.price != self._initial_price:
self.create = datetime.now(UTC)
super().save(*args, **kwargs)
You can overwrite __init__ and save on the model as an easy alternative
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._price_dirty = self.price
def save(self, *args, **kwargs):
if self._price_dirty != self.price:
self.create = timezone.now()
super().save(*args, **kwargs)

Generic UpdateView isn't working with slug

I'm pretty new to django.I am trying to update a post with generic UpdateView. But the post isn't updating after filling up the form.Im accessing the update view through slug url.
My model:
class Post(models.Model):
title = models.CharField(max_length=60)
post_body = models.TextField()
time_posted = models.DateTimeField(default=timezone.now)
author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete= models.CASCADE)
slug = models.SlugField(null=False,unique=True)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('postdetail', kwargs={'slug': self.slug})
def save(self,*args,**kwargs):
if not self.slug:
author_id = str(self.author.id)
self.slug = slugify(self.title +'-'+author_id)
return super().save(*args, **kwargs)
My view:
class postupdate(LoginRequiredMixin,UserPassesTestMixin,UpdateView):
model = Post
fields = ['title','post_body']
def form_valid(self, form):
form.instance.author = self.request.user
return super().form_valid(form)
def test_func(self):
post = self.get_object()
if self.request.user == post.author:
return True
else:
return False
My url:
path('post/<slug:slug>/updatepost/', postupdate.as_view(),name = 'updatepost'),
It seems the save() method is not getting called every time.
class Post(models.Model):
# rest of your code
def save(self, *args, **kwargs):
if not self.slug:
author_id = str(self.author.id)
self.slug = slugify(self.title + '-' + author_id)
return super().save(*args, **kwargs) # outside the `if...` clause

Cannot Edit model fields/Django Signal to update image likes wont work/

I implemented a signal with an m2mchanged receiver to update a PositiveIntegerField in my Image model. This PositiveIntegerField is for total_likes of the image but upon implementing the signal I realized that the total_likes field did not change from zero. I started experimenting and found that this value will not change even if I change it in the shell or the admin page itself.
I dont think this is a problem with the signal/receiver but rather something else happening that wont let the value be change in the database.
Here is My code.
models.py
class Image(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL,
related_name='images_created')
title = models.CharField(max_length=200)
slug = models.SlugField(max_length=200,
blank=True)
url = models.URLField()
image = models.ImageField(upload_to='images/%Y/%m/%d')
description = models.TextField(blank=True)
created = models.DateField(auto_now_add=True, db_index=True)
def __str__(self):
return self.title
def save(self, *args, **kwargs):
if not self.slug:
self.slug = slugify(self.title)
super(Image, self).save(*args, *kwargs)
users_like = models.ManyToManyField(settings.AUTH_USER_MODEL,
related_name='images_liked',
blank=True)
def get_absolute_url(self):
return reverse('images:detail', args=[self.id, self.slug])
tags = TaggableManager()
total_likes = models.PositiveIntegerField(db_index=True, default=0)
I can post the signals.py files or any other code if necessary.
Thank you!
Solution
My problem was in my overridden save method, I changed it to the following.
def save(self, *args, **kwargs):
if not self.slug:
self.slug = slugify(self.title)
super(Image, self).save(*args, *kwargs)
elif self.slug != slugify(self.title):
self.slug = slugify(self.title)
super(Image, self).save(*args, *kwargs)
else:
super(Image, self).save(*args, *kwargs)
Correct and simple solution here will be:
def save(self, *args, **kwargs):
if not self.id: # don't allow to change slug after object created
self.slug = slugify(self.title)
super(Image, self).save(*args, **kwargs)

optimizing django code model

I want to optimizing this code. I think the best solution is to use the method pre_save and not override the save method. This is the function that delete the old image when in editing it upload new image
def delete_old_image(sender, instance):
try:
obj = sender.objects.get(id=instance.id)
except sender.DoesNotExist:
pass
else:
if not obj.image == instance.image:
try:
os.remove(obj.image.path)
except:
pass
under the code of the model
class Service(models.Model):
title= models.CharField(max_length=170)
slug = models.SlugField(max_length=200, blank=True, unique=True, editable=False)
def save(self, *args, **kwargs):
self.slug = slugify(self.title)
super(Service, self).save(*args, **kwargs)
class Portfoglio(models.Model):
title= models.CharField(max_length=170, unique=True)
slug = models.SlugField(max_length=200, blank=True, unique=True, editable=False)
image=models.ImageField(upload_to = 'images/' , default= 'images/foto.jpg', verbose_name='upload')
def save(self, *args, **kwargs):
self.slug = slugify(self.title)
if self.id is not None:
delete_old_image(Portfoglio, self)
super(Portfoglio, self).save(*args, **kwargs)
class Image(models.Model):
title= models.CharField(max_length=200)
image=models.ImageField(upload_to = 'images/' , default= 'images/foto.jpg', verbose_name='upload')
def save(self, *args, **kwargs):
if self.id is not None:
delete_old_image(Portfoglio, self)
super(Image, self).save(*args, **kwargs)
class Team(models.Model):
name= models.CharField(max_length=200)
image= models.ImageField(upload_to = 'images/' , default= 'images/foto.jpg', verbose_name='upload')
def save(self, *args, **kwargs):
if self.id is not None:
delete_old_image(Team, self)
super(Team, self).save(*args, **kwargs)
from django.db.models.signals import pre_save
from django.dispatch import receiver
#receiver(pre_save)
def pre_delete_old_image(sender, instance, created, **kwargs):
if sender not in [Service, Portfoglio, Image, Team]:
return
if getattr(sender, 'slug', False):
instance.slug = slugify(instance.title)
if not created and getattr(sender, 'image', False):
delete_old_image(sender, instance)
def delete_old_image(sender, instance):
try:
obj = sender.objects.get(id=instance.id)
except sender.DoesNotExist:
pass
else:
if obj.image != instance.image:
try:
os.remove(obj.image.path)
except Exception as e:
pass