i had made the changes in model .py but its not working
here is model.py
from django.db import models
import django.forms as forms
from virtualenv import REQUIRED_FILES
from _mysql import NULL
from datetime import datetime, date, time, timedelta
import time
import calendar
from django.contrib.auth.models import User
import os
from uuid import uuid4
from django.core.files.storage import default_storage as storage
from django.db.models.signals import pre_save
def path_and_rename(path):
def wrapper(instance, filename):
ext = filename.split('.')[-1]
# get filename
if instance.pk:
filename = '{}.{}'.format(instance.pk, ext)
else:
# set filename as random string
filename = '{}.{}'.format(uuid4().hex, ext)
# return the whole path to the file
return os.path.join(path, filename)
return wrapper
def image_delete(sender, instance, *args, **kwargs):
if instance.pk:
old_instance = User.objects.get(pk=instance.pk)
if old_instance.image != instance.image and instance.image:
storage.delete(old_instance.image.name)
pre_save.connect(image_delete, sender=User)
class Publisher(models.Model):
name = models.CharField(max_length=30)
address = models.CharField(max_length=50)
city = models.CharField(max_length=60)
state_province = models.CharField(max_length=30)
country = models.CharField(max_length=50)
website = models.URLField()
def __unicode__(self):
return self.name
class Author(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=40)
email = models.EmailField()
def __unicode__(self):
return u'%s %s' % (self.first_name, self.last_name)
class Book(models.Model):
title = models.CharField(max_length=100)
authors = models.ManyToManyField(Author)
publisher = models.ForeignKey(Publisher)
publication_date = models.DateField()
def __unicode__(self):
return self.title
class User(models.Model):
first_name=models.CharField(verbose_name = "First Name *",max_length=20,blank=False)
last_name=models.CharField(verbose_name = "Last Name *",max_length=20,blank=False)
username=models.EmailField(verbose_name = "Email *",max_length=30,blank=False)
password=models.CharField(verbose_name = "Password *",max_length=15,blank=False)
dob=models.DateField(verbose_name = "DOB *" ,blank=True,null=True)
mobileno=models.CharField(verbose_name = "Mobile No *",max_length=20,blank=False)
houseno=models.CharField(verbose_name = "House No",max_length=10,blank=True)
address1=models.CharField(verbose_name = "Adress1",max_length=30,blank=True)
city=models.CharField(verbose_name = "City",max_length=20,blank=True)
state=models.CharField(verbose_name = "State",max_length=30,blank=True)
pincode=models.CharField(verbose_name = "Pincode",max_length=20,blank=True)
country=models.CharField(verbose_name = "Country",max_length=30,blank=True)
comment=models.CharField(verbose_name = "Comment",max_length=200,blank=True)
sex=models.CharField(verbose_name = "Sex *",max_length=5,blank=False)
image=models.FileField(verbose_name = "Image(limit 1Mb) *",blank=True,upload_to=path_and_rename(''))
def __unicode__(self):
return self.first_name
On Tue, Mar 24, 2015 at 6:47 PM, Akash Patni <akash.patni@ranosys.com> wrote:
is there any other changes that i have to made in model like how this above mentioned function will be call.Please suggestOn Tue, Mar 24, 2015 at 6:43 PM, Andreas Kuhne <andreas.kuhne@suitopia.com> wrote:Hi,--You shouldn't do it in the view, do it in the database model instead. It won't do anything to any other fields than the image field the way I wrote the function (all other fields are untouched).The reason you shouldn't do it in the view, is because if you were to update the image any other place than via the view, the code wouldn't run. It's more sensible and more DRY to do it in the model.Regards,Andréas2015-03-24 14:10 GMT+01:00 Akash Patni <akash.patni@ranosys.com>:To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CANSMZiTGE4rue_LtY3YrsERGqBuy%3DoFX0eJb_9o81R_us-OibQ%40mail.gmail.com.Hey,I am new to django. I have other feilds also other than image field in my form, so will you please tell me how to use above mentioned function in edit_profile function in view, Can i call another function from another function in views.py, if yes please tell me--On Tue, Mar 24, 2015 at 6:11 PM, Andreas Kuhne <andreas.kuhne@suitopia.com> wrote:--Hi,Ok, so what you should do is add a signal to pre save and then delete the old image if the new image is attached.Something like this should work:from django.core.files.storage import default_storage as storage
from django.db.models.signals import pre_savedef image_delete(sender, instance, *args, **kwargs):if instance.pk:old_instance = User.objects.get(pk=instance.pk)if old_instance.image != instance.image and instance.image:storage.delete(old_instance.image.name)pre_save.connect(image_delete, sender=User)You can see more information about signals here: https://docs.djangoproject.com/en/1.7/topics/signals/Regards,Andréas2015-03-24 11:40 GMT+01:00 <akash.patni@ranosys.com>:When i edit image that is when i upload new image,the old image should get deleted--
On Tuesday, March 24, 2015 at 2:40:35 PM UTC+5:30, akash...@ranosys.com wrote:hi..Can anyone please tell me how to handle old image after uploading new image.
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/ca9fa002-cbd5-4bbf-b42c-2a6ce88e8e0c%40googlegroups.com.
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/oZrAdWgzwo0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CALXYUbkpM6op303LMxA2iGNuzqgzB%3DjBPWw2KXkpjwdaho2Z%2BQ%40mail.gmail.com.--Software EngineerBest Regards,Akash Patni
Head Office:
Oxley Bizhub, #06-48 | 73 Ubi Road 1 | Singapore - 408733
Tel: +65 66331556 | HP: +65 98573420
Global Offices:
San Francisco, USA | Jaipur, India | Bikaner, India
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/oZrAdWgzwo0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CALXYUb%3DxUnSdyTevvw3oSwA%2B1kpvMwBXednRxkswXMUkMdU78g%40mail.gmail.com.--Software EngineerBest Regards,Akash Patni
Head Office:
Oxley Bizhub, #06-48 | 73 Ubi Road 1 | Singapore - 408733
Tel: +65 66331556 | HP: +65 98573420
Global Offices:
San Francisco, USA | Jaipur, India | Bikaner, India
Best Regards,
Akash Patni| | |||||||||
Head Office:
| |||||||||
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CANSMZiRtfvrhxmUrw-GM4dsbakkM7ccONbHDVA4y4NCywX3aFA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment