Question

I'm trying to take input of a domain name (domainNm) and an email address at a domain (emailVerified) and submit them via modelform based off a table [Tld] .

It appears, it is failing to save() the foreign key (from the currently authenticated user)

domain.FKtoClient = user_info

What am I doing wrong here?

forms.py

class SubmitDomain(ModelForm):
    domainNm = forms.CharField(initial=u'', label='Enter your domain')
    emailVerified = forms.EmailField(label='Email at Domain')

    class Meta:
        model = Tld #Create form based off Model for Tld
        fields = ['domainNm','emailVerified']

    def save(self, request=None):
        instance = self.cleaned_data
        #domain =  instance["domainNm"])
        domains = Tld.objects.filter(domainNm=instance["domainNm"])
        if len(domains) == 0:
            domain = Tld()
        else:
            domain = domains[0]
        user_info = request.user
        unique_id = int(uuid.uuid4())
        domain.generated_hash = str(unique_id)
        domain.entered_email = instance["emailVerified"]
        domain.domainNm = instance["domainNm"]
        domain.FKtoClient = user_info
        domain.save()

Thanks!

Was it helpful?

Solution

def save(self, request=None):

You assign a default value of None to request in the definition of save, so what happens when the caller of save doesn't pass an instantiated request?

user_info = request.user #request is None here

That will throw the error you see. To mitigate, add a simple if request is not None or similar statement.

EDIT

After seeing your views.py, you are passing request.POST to SubmitDomain's __init__ magic method, which you have not defined. The way you have your modelform defined above, you would have to pass the request to save(), not __init__(), i.e.

form.save(request)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top