I have found the right solution. Thanks
Hello,i have following:models.py:from django.db import modelsclass Tabs(models.Model):id = models.AutoField(primary_key=True)def __str__(self):return self.nameclass Box(models.Model):id = models.AutoField(primary_key=True)tabslink = models.ForeignKey(Tabs, on_delete=models.CASCADE, null=False,blank=False)def __init__(self, *args, **kwargs):super().__init__(*args, **kwargs)print([field.name for field in self._meta.get_fields()])for field in [field.name for field in self._meta.get_fields()]:getattr(self, field)views.py:--from django.views.generic.edit import (CreateView,)from django.urls import reverse, reverse_lazyfrom django import formsclass Boxform(forms.ModelForm):class Meta:model = Boxfields = ['tabslink']class test(CreateView):model = Boxtemplate_name = 'test.html'form_class = Boxformsuccess_url = reverse_lazy('Boxlistview')Now i get a Exception:Exception Value: Box has no tabslink./home/sebastian/PycharmProjects/test3/test4/models.py, line 22, in __init__getattr(self, field) …▶ Local vars/home/sebastian/.local/lib/python3.8/site-packages/django/db/models/fields/related_descriptors.py, line 197, in __get__raise self.RelatedObjectDoesNotExist( …▶ Local varsThis happens only on fields that have a models.ForeignKey entry...Please help me that i can also get related with getattr.Regards
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 view this discussion on the web visit https://groups.google.com/d/msgid/django-users/b672baf2-4e87-49a2-ac17-ba6daff0cb9bn%40googlegroups.com.
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 view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAKGT9myLx_wj399%2Bem1%2BgPFgKMW_eJJ60-84rmMyP_OyrugB3Q%40mail.gmail.com.
No comments:
Post a Comment