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.fieldsfrom 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 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 django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
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.
For more options, visit https://groups.google.com/d/optout.
File "C:\Users\ ... xxxxxxx ... \items\views.py", line 7, in ItemUpdateView
fields = self.model.fields
NameError: name 'self' is not defined
Regards,Andréas
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/95db9cca-58ca-4460-ae39-9150bb199bff%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/c857567f-dd71-46c1-a05b-787566bfa180%40googlegroups.com.
def dispatch(self, request, *args, **kwargs):
# Try to dispatch to the right method; if a method doesn't exist,
# defer to the error handler. Also defer to the error handler if the
# request method isn't on the approved list.
if request.method.lower() in self.http_method_names:
handler = getattr(self, request.method.lower(), self.http_method_not_allowed)
else:
handler = self.http_method_not_allowed
return handler(request, *args, **kwargs)To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/b6502b29-81e6-449e-894b-16f92ed5e871%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.