Hi, Have you tried self.model? It should be present in all methods in the class?
Regards, Andréas 2018-06-05 21:56 GMT+02:00 Mikkel Kromann <[email protected]>: > Dear Django-users. > > I'm slowly working towards a Django "data-warehouse" framework, where I > can easily add a lot of models (each containing a table with data), while > sticking to only few reusable views. > > I've realised that in urls.py, it is possible to specify the model on > which the UpdateView is going to do its magic. > This allows me to use a generic UpdateView (which I called ItemUpdateView) > and template for all my models, which will save me a ton of almost > identical lines of code. > > However, in the generic ItemUpdateView, I of course need to specify the > fields of the specific model chosen in urls.py. > As I ideally only want a few generic views (i.e. ItemCreateView, > ItemUpdateView, ItemDeleteView and ItemListView), I've chosen to place the > field array in my model definition, hoping to be able to access it from my > generic Class Based Views > > But how do I access the model attribute in (Item)UpdateView > > > thanks, Mikkel > > From views.py > from django.views.generic import CreateView, ListView, UpdateView, > DeleteView > > class ItemUpdateView(UpdateView): > template_name = "item_form.html" > # How do I access the model attribute of ItemUpdateView as given in > urls.py? > # This line below returns the error "NameError: name 'model' not defined" > fields = model.fields > > From urls.py > from django.urls import path > from . views import ItemUpdate > from . models import Region, Location > > # Awesome! I can specify the model to be used by ItemUpdateView > urlpatterns = [ > path('update/region/<pk>', ItemUpdateView.as_view(model=Region), > name='region_update'), > path('update/location/<pk>', ItemUpdateView.as_view(model=Location), > name='location_update'), > ] > > > > From models.py (Region and Location are only two of my tables, I'd like to > have say 20 or 30 models > from django.db import models > > # Abstract class for our items including common methods, data and > definitions > class ItemModel(models.Model): > > fields = [ 'label', 'short', 'descr' ] > label = models.CharField(max_length=10) > short = models.CharField(max_length=15) > descr = models.CharField(max_length=40) > > def __str__(self): > return self.short > > class Meta: > abstract = True > > class Region(ItemModel): > fields = [ 'label', 'short', 'descr' ] > > class Location(ItemModel): > fields = [ 'label', 'short', 'descr', 'region' ] > region = models.ForeignKey(Region, on_delete=models.CASCADE) > > class Plant(ItemModel): > fields = [ 'label', 'short', 'descr', 'location', 'capex', 'opex', > 'capacity' ] > location= models.ForeignKey(Location, on_delete=models.CASCADE) > capex = models.DecimalField(decimal_places=3, max_digits=8) > opex = models.DecimalField(decimal_places=3, max_digits=8) > capacity= models.DecimalField(decimal_places=3, max_digits=8) > > > -- > 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 [email protected]. > To post to this group, send email to [email protected]. > Visit this group at https://groups.google.com/group/django-users. > To view this discussion on the web visit https://groups.google.com/d/ > msgid/django-users/ec4634e5-2279-49a7-9045-21712de87584%40googlegroups.com > <https://groups.google.com/d/msgid/django-users/ec4634e5-2279-49a7-9045-21712de87584%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 [email protected]. To post to this group, send email to [email protected]. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAK4qSCejxaTZW1j--Pmtjn1CYvexvNXZ-9YN4wyuiv4jBjcNUQ%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.

