I know this has already been discussed several times, I found several posts about it through Google but I'm still interested in getting your opinion on an example.
I'm wondering that because my models file is getting big. That makes me confused so I'm wondering if I'm doing the right thing from a design point of view.
I have the feeling that my models should remain simple. What do you think ?
For example, let's say I want to create a model named Feed. (simplified version)
class Feed(models.Model):
name = models.CharField(max_length=255, unique=True)
url = models.URLField(unique=True)
etag = models.CharField(max_length=255, null=True, blank=True)
I want to be able to extract a feed (that is to say to download it and store it(as a file but I also keep a track in the DB through a File model)). Would you create:
- an extract method in the model (this is what I have for now, I simplified it, it is much bigger actually. I have some try..except block to log some possible errors, etc. And my _store function is pretty big too because it saves in the storage and in the DB.
def extract(self):
data, etag = utils.download_from_url(self.url, self.etag) #Download from the url
self._store(data) #Store it
self.etag = etag # Change the etag
self.save()
to use like that:
f = Feed.objects.get(pk=1)
f.extract()
- a view:
that I could access through http://127.0.0.1:8000/feed/extract/1
- a "util" function to whom I pass the Feed object.
f = Feed.objects.get(pk=1)
utils.extract_feed(f)
Thanks for your advice.
Michael
-- You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/JAFZ8vzNTuQJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
No comments:
Post a Comment