Hello,

i have following:

models.py:

from django.db import models

class Tabs(models.Model):
    id = models.AutoField(primary_key=True)
    def __str__(self):
        return self.name

class 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_lazy
from django import forms

class Boxform(forms.ModelForm):
    class Meta:
        model = Box
        fields = ['tabslink']

class test(CreateView):
    model = Box
    template_name = 'test.html'
    form_class = Boxform
    success_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 vars


This 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+unsubscr...@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.

Reply via email to