I have a model called Link which allows users to send url's. And there are some Generic Views to handle CRUD operations.
Then I've decided to add another model called Image which allows users to send images. Since there are multiple shared field between Link and Image I created new abstract model to keep common fields in it.
Now I should add views for the Image model. The naive option seems to be duplicating all generic views for Link and replace model fields with Image. But I guess there might be a more efficient way to do it (like the abstract model to avoid duplicating shared fields). I'm not sure but maybe using ContentType
module would help?
So here's my question: Am I right about using ContentType builtin application to handle CRUD operations for both models to be efficient? If so, how would I write the views? For example at the moment I explicitly define the model to be used by generic view, how would I do it when I want the same view to do same task for Two different models? Or maybe using generic views in this case is not the way to go?
I add some part of my code so if anyone would answer this questions by some short code hints could use them:
models.py
class Base(models.Model):
title = models.CharField(max_length=200)
slug = models.CharField(max_length=200, db_index=True)
category = models.ForeignKey(Category, default=1)
. . .
class Meta:
abstract = True
class Link(Base):
url = models.URLField("URL")
description = models.TextField(max_length=500)
...
class Image(Base):
image = models.ImageField()
...
views.py
class LinkListView(ListView):
model = Link
queryset = Link.objects.all().order_by('-rank_score')
paginate_by = 10
class LinkDetailView(FormMixin, DetailView):
models = Link
queryset = Link.objects.all()
...