Django: How to set a hidden field on a generic create view?

前端 未结 3 1358
情深已故
情深已故 2021-01-06 09:31

I\'m running Django 1.6.x

To extend my user I\'ve added another model storing the data:

class UserProfile (models.Model):
    user = models.ForeignK         


        
3条回答
  •  不要未来只要你来
    2021-01-06 10:20

    To avoid gottcha's like must be a “User” instance error you would want to try this.

     def form_valid(self, form):
            owner = self.request.user
            print("Bot owner1", owner)
            tenant = get_object_or_404(Tenant, user=owner)
            print("Bot owner2 ", tenant)
            form.instance.tenant = tenant
    
        return super().form_valid(form)
    

    Turned out that

    print("Bot owner1", owner)
    

    and

    print("Bot owner2 ", tenant)
    

    have different results.

    Any other approach besides the form_valid method could pose a security risk. i.e I could edit the initial value by inspecting element on your browser and put my new value. If your code does not validate my entry Bang!!!Bang!! you're hacked.

提交回复
热议问题