更新具有ForeignKey配置文件字段的模型的字段

时间:2018-10-31 22:28:39

标签: django django-models django-forms django-views

当我更新有效的管理员创建的用户created_by用户的信息时,但是当我从另一个用户登录并创建了一个由该登录用户创建但创建的用户时,我有3种类型的用户admin,分发者,客户更新该新用户的信息,我得到没有配置文件的错误告诉我。 请帮我 enter image description here

**views.py **
@login_required(login_url="/account/login/")
def edit_distributor(request,id=None):
    instance = get_object_or_404(Distributor, id=id)

    distributor_form = Distributorform(data=request.POST, instance=instance)




    if request.method == "POST":
        if  distributor_form.is_valid():
            # add_balance = distributor_form.cleaned_data.get('add_solde'):


            distributor = distributor_form.save(commit=False)
            distributor.created_by = request.user
            distributor.balance = distributor.balance + 10





            distributor_form.save()




            return redirect('all_distriubtor')



        else:
            messages.error(request, "Error", extra_tags='alert')
            return redirect('edit_distriubtor', id)

    else:

        distributor_form = Distributorform(instance=instance)





    return render(request,"UsersManager/edit_distributor.html",{ "instance":instance,

                                                                 "distributor_form":distributor_form,

                                                                 })

**models.py**
    class Profile(models.Model):
    user = models.OneToOneField(User,on_delete=models.CASCADE)
    phone = models.CharField(max_length=100)
    newpassword = models.CharField(blank=True,max_length=100)
    codePin = 
    models.CharField(blank=True,max_length=150,unique=False,default="")
    is_customer = models.BooleanField(default=False)
    is_Distributor = models.BooleanField(default=False)


    def __str__(self):
        return self.user.first_name





@receiver(post_save, sender=User)
def user_is_created(sender,instance,created,**kwargs):
    if created:
        Profile.objects.create(user=instance)
    else:
        instance.profile.save()

Class Customer(models.Model):

balance = models.DecimalField(max_digits=12,decimal_places=2)
created_by = models.ForeignKey(User,on_delete=models.CASCADE,blank=True)
profile = models.ForeignKey(Profile,on_delete=models.CASCADE,blank=True)
created = models.DateField(null=True,blank=True)
updated = models.DateTimeField(null=True, blank=True)

def save(self):
    if not self.id:
        self.created = datetime.date.today()
    self.updated = datetime.datetime.today()
    super(Customer,self).save()

modles.py

class Distributor(models.Model):

    balance = models.DecimalField(max_digits=12,decimal_places=2)
    created_by = models.ForeignKey(User,on_delete=models.CASCADE,blank=True)
    profile = models.ForeignKey(Profile,on_delete=models.CASCADE,blank=True)
    created = models.DateField(null=True, blank=True)
    updated = models.DateTimeField(null=True, blank=True)

    def save(self):
        if not self.id:
            self.created = datetime.date.today()
        self.updated = datetime.datetime.today()
        super(Distributor, self).save()

0 个答案:

没有答案