> an email to django-users+unsubscribe@googlegroups.com
> <mailto:django-users+unsub...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto: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/bf9f6ff3-a35a-4f00-8537-c6fd66d07f8d%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/bf9f6ff3-a35a-4f00-8537-c6fd66d07f8d%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.
--
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...@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/54BCB642.8040201%40dewhirst.com.au.
I second moving some (or most) of the functionality to models.py. For instance, calculating the SHA value should be done as part of the model's save() function, not done in the view.
Convention dictates that the only code that is placed in the view itself should be logic related to prepping the templates for rendering/displaying things, such as setting variables in the template context.
Any operations related to 'business logic' such as data manipulation (ie save behavior) as it relates to specific objects should be handled by the object/model itself. Ideally, the model instance should be smart enough to save itself (or perform some other action) on its own without any input from the view, assuming the necessary values for the operation are provided from some other source.
Obviously this is only a convention and not a rule, but it will likely serve you better in the long run. In general, views tend to be quite short. My class-based views just have a couple of class-level attributes set, if anything at all (although they rely heavily on inherited behavior from parent class views, which are much more complicated).
-James
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@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/CACPst9JZnMGK8vS00G1oqwEVt9qbf3TnTsdoJCwdy-mN02qEPw%40mail.gmail.com.
> an email to django-users...@googlegroups.com
> <mailto:django-users+unsubscrib...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@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/bf9f6ff3-a35a-4f00-8537-c6fd66d07f8d%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/bf9f6ff3-a35a-4f00-8537-c6fd66d07f8d%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.
--
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...@googlegroups.com.
I wouldn't decorate them as class methods. You would want to call them from the objects themselves. For the save_to_disk() method, I was actually referring to the Django save() method (https://docs.djangoproject.com/en/1.7/topics/db/models/#overriding-predefined-model-methods).
As you have it now, it wouldn't work since you would need to call UploadFile.generate_id(), but you don't have an available argument to pass in the UploadFile object to get the ID for.
If you remove the decorator, you would call it on the object itself:
# override the model save method to generate the hash ID, then save for real
def save(self):
self.generate_id()
super(UploadFile, self).save(*args, **kwargs)
You probably don't need to change your view logic at all in this case (unless you have code that generates the hash and attaches it to the object, then remove it and let the object handle that itself). Basically, when your UploadFile object is saved, it will call the save() method on the object, which in turn will generate the ID based on the file, attach it to the object, and then save the object to the database.
Note that this will run the hashing mechanism every time the object is saved, whether the file it points at changes or not. If this isn't what you want, I would add an extra checks in generate_id() to account for all of the scenarios where you wan the hash to update (every time the object is saved vs. only when the file changes vs. only generated once even with a file change, etc.).
In a typical scenario where the hash is run every time the object is saved, the object itself would create the hash. This way you can save the object from any view (or from the shell), and it would always generate a new hash automagically (new meaning recalculated, may end up the same as the previous value).
TL;DR; The hash shouldn't be calculated external to the file that is attached to the object, it should be calculated by the object itself.
Hope that helps and makes sense...
-James
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/80aef78c-9cec-4e50-be11-bc506d019b8c%40googlegroups.com.
Hello,I am new to Django and I have run into an issue with views.py. I understand that there is a function behind each view. For a view that I am currently writing, it accepts a file upload from user and stores the file on the server. To do that, I need to:1. check the file extension is correct2. generate a SHA-1 id for the file based on its content3. write the file to disk4. save information about the file to database(oh, I also created two global variables as well)
class UploadFile(models.Model):
upload_date = models.DateTimeField('timestamp', auto_now_add=True)
original_file_name = models.CharField(max_length=200)
file_id = models.CharField(max_length=40, primary_key=True)
file_path = models.CharField(max_length=500)
owner = models.ForeignKey(User)