RelatedObjectDoesNotExist at /add-company/

    
    
    
    
    
    
    
    
    
    
    
    
  
    










I am creating page like UserProfile using django signals


hello sir, 

I am creating One Page Company Profile, User has permission to create One Company only. When I open Company Profile page

This error occurs  

RelatedObjectDoesNotExist at /add-company/

CustomUser has no companymaster.

But when I fill the form from admin page submitted data is how in text box like this

see image here

 

how i update values in this form and if company is not added user will create company(he/she see the empty form)

 

 

 

########models.py

class CompanyMaster(models.Model):
    user                = models.OneToOneField(CustomUser, on_delete=models.CASCADE,
                          related_name='companymaster')
    Name                = models.CharField(max_length=50)
    Address             = models.CharField(max_length=100)
    City                 = models.CharField(max_length=30)
    State                 = models.CharField(max_length=30)
    Country             = CountryField()
    LandlineNumber         = models.CharField(max_length=20)
    MobileNumber         = models.CharField(max_length=13)
    EmailAddress         = models.CharField(max_length=50)
    WebsiteAddress         = models.URLField(max_length=50

 

########views.py

@login_required
def CompanyMasterCreateView(request):

    # profile = CompanyMaster.objects.get(request.user)
    # print(request.user.companymaster)
    # print("****************")
    if request.method == 'POST':

        form = CompanyMasterForm(request.POST, instance=request.user.companymaster)
        if form.is_valid():
            form.save()
    else:
        form = CompanyMasterForm(instance=request.user.companymaster)

    context = {'form': form }

    return render(request,'master/companymaster_form.html', context)


##########signals.py

 

 

from django.db.models.signals import post_save
from users.models import CustomUser
from django.dispatch import receiver
from .models import CompanyMaster

@receiver(post_save, sender=CustomUser)
def create_companymaster(sender, instance, created, **kwargs):
    if created:
        CompanyMaster.objects.create(user=instance)
post_save.connect(create_companymaster, sender=CustomUser)


@receiver(post_save, sender=CustomUser)
def save_companymaster(sender, instance, **kwargs):
    instance.CompanyMaster.save()
 

 

 

###apps.py 

 

please sir slove this problem

 

Category: Django
Asked at Nov. 23, 2018, 6:07 a.m. by patilganeshv

0 Answers