首页 > 解决方案 > 如何在 django 中更新卖家资料?

问题描述

我正在开发一个具有两种类型用户的多用户项目。

第一个是卖方,第二个是买方。当卖家注册自己时,它将被重定向到他的个人资料,卖家可以通过提供名字、姓氏、地址等额外凭据来更新其个人资料,但我遇到的主要问题是,当我提供要更新的数据时它什么也没显示。

所以我请求大家为我提供最好的解决方案。我必须在周日晚上之前提交这项工作。

我还为您提供了我的代码,以便大家都能理解我错在哪里。

models.py

from django.db import models
from accounts.models import Seller


class Profile(models.Model):
    CATEGORY_CHOICES = (
        ('M', 'Male'),
        ('F', 'Female'),
    )
    first_name = models.CharField(max_length=200)
    last_name = models.CharField(max_length=200)
    profile_image = models.ImageField(upload_to='photos/')
    date_of_birth =  models.DateField()
    gender = models.CharField(max_length=200, choices=CATEGORY_CHOICES)
    email = models.EmailField(max_length=200)
    phone = models.CharField(max_length=200)
    address = models.CharField(max_length=200)
    # city = models.CharField(max_length=200)
    # number = models.CharField(max_length=200)
    # zip = models.CharField(max_length=200)
    # country = models.CharField(max_length=200)
    seller = models.ForeignKey(Seller, on_delete=models.CASCADE)

    def __str__(self):
        return self.name

注意:我有另一个名为 accounts 的应用程序,其中我有两个模型 1st Seller 和 2nd BuyerOneToOneUser.

计算应用模型:

class Buyer(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, null=True, related_name='buyer_profile')
    bio = models.CharField(max_length=30, blank=True)
    location = models.CharField(max_length=30, blank=True)
    first_name = models.CharField(max_length=200, null=True, blank=True)
    last_name = models.CharField(max_length=200, null=True, blank=True)
    profile_image = models.ImageField(upload_to='photos/', null=True, blank=True)
    date_of_birth =  models.DateField(null=True, blank=True)
    phone = models.CharField(max_length=200, null=True, blank=True)
    address = models.CharField(max_length=200, null=True, blank=True)
    gender = models.CharField(max_length=200, null=True, blank=True)

    def __str__(self):
        if self.user:
            return str(self.user.username)
        else:
            return 'Buyer Name is null'


class Seller(models.Model):
    CATEGORY_CHOICES = (
        ('M', 'Male'),
        ('F', 'Female'),
    )
    user = models.OneToOneField(User, on_delete=models.CASCADE, null=True, related_name='seller_profile')
    company_name = models.CharField(max_length=100, blank=True)
    website = models.CharField(max_length=100, blank=True)
    location = models.CharField(max_length=30, blank=True)
    first_name = models.CharField(max_length=200, null=True, blank=True)
    last_name = models.CharField(max_length=200, null=True, blank=True)
    profile_image = models.ImageField(upload_to='photos/', null=True, blank=True)
    date_of_birth =  models.DateField(null=True, blank=True)
    phone = models.CharField(max_length=200, null=True, blank=True)
    address = models.CharField(max_length=200, null=True, blank=True)
    gender = models.CharField(max_length=200, null=True, blank=True, choices=CATEGORY_CHOICES)

    def __str__(self):
        if self.user:
            return str(self.user.username)
        else:
            return 'Seller Name is null'

views.py

def seller_update_view(request):
    user = request.user
    seller = request.user.profile # I also get error here. 
    print("Update profile",seller)
    user_form = UserUpdateForm(instance=user)
    profile_form = ProfileUpdateForm(instance=seller)
    if request.method == 'POST':
        user_form = UserUpdateForm(request.POST, instance=user)
        profile_form = ProfileUpdateForm(request.POST, request.FILES, instance=seller)
        if user_form.is_valid() and profile_form.is_valid():
            user_form.save()
            profile_form.save()
            return redirect('settings')
    return render(request, 'settings.html', {
            'user_form': user_form,
            'profile_form': profile_form,
        })

forms.py

from InMay.models import Profile, Event
from django import forms
from accounts.models import User  # I am importing it from accounts app

class ProfileUpdateForm(forms.ModelForm):

    class Meta:
        model = Profile
        fields = ['first_name', 'last_name', 'profile_image', 'date_of_birth','gender', 'phone', 'address']


class UserUpdateForm(forms.ModelForm):
    class Meta:
        model = User
        fields = ['email', 'username']

settings.html

{% extends 'base.html' %}
{% load static %}
{% block content %}
    <main>
        <div class="preloader bg-dark flex-column justify-content-center align-items-center">
    <div class="position-relative">
        <img src="{% static 'assets/img/brand/light-without-letter.svg' %}" alt="Logo loader">
        <img src="{% static 'assets/img/brand/letter.svg' %}" class="rotate-letter" alt="Letter loader">
    </div>
</div>

        <div class="section section-lg bg-soft">
            <div class="container">
                <div class="row pt-5 pt-md-0">
                
                    <div class="col-12 col-md-4 d-none d-lg-block">
    <!-- Navigation -->
    <div class="card border-light p-2">
        <div class="card-body p-2">
            <div class="profile-thumbnail small-thumbnail mx-auto">
                <img src="{% static 'assets/img/team/profile-picture-4.jpg' %}" class="card-img-top rounded-circle border-white" alt="Joseph Portrait">
                <img src="{{user.seller.profile_form.profile_image.url}}" class="card-img-top rounded-circle border-white" alt="Dynamic Image">
            </div>
            <h5 class="h5 font-weight-normal text-center mt-3 mb-0">{{profile_form.first_name}} {{profile_form.last_name}}</h5>
            <div class="list-group dashboard-menu list-group-sm mt-4">
                <a href="./account.html" class="d-flex list-group-item list-group-item-action ">Overview <span class="icon icon-xs ml-auto"><span class="fas fa-chevron-right"></span></span> </a>
                <a href="" class="d-flex list-group-item list-group-item-action  active ">Settings<span class="icon icon-xs ml-auto"><span class="fas fa-chevron-right"></span></span> </a>
                <a href="./my-items.html" class="d-flex list-group-item list-group-item-action ">My Items<span class="icon icon-xs ml-auto"><span class="fas fa-chevron-right"></span></span> </a>
                <a href="./security.html" class="d-flex list-group-item list-group-item-action ">Security<span class="icon icon-xs ml-auto"><span class="fas fa-chevron-right"></span></span> </a>
                <a href="./billing.html" class="d-flex list-group-item list-group-item-action ">Billing<span class="icon icon-xs ml-auto"><span class="fas fa-chevron-right"></span></span> </a>
                <a href="./messages.html" class="d-flex list-group-item list-group-item-action  border-0">Messages<span class="icon icon-xs ml-auto"><span class="fas fa-chevron-right"></span></span> </a>
            </div>
        </div>
    </div>
</div>
<div class="col-12 d-lg-none">
    <div class="card bg-white border-light mb-4 mb-lg-5">
        <div class="card-body">
            <div class="row align-items-center">
                <div class="col-10 d-flex">
                    <a href="./account.html" class="list-group-item list-group-item-action border-0 ">Overview</a>
                    <a href="{% url 'settings' %}" class="list-group-item list-group-item-action border-0  active ">Settings</a>
                    <a href="./my-items.html" class="list-group-item list-group-item-action d-none d-sm-block border-0 ">My Items</a>
                    <a href="./security.html" class="list-group-item list-group-item-action d-none d-md-block border-0 ">Security</a>
                </div>
                <div class="col-2 d-flex justify-content-center">
                    <div class="btn-group dropleft">
                        <button class="btn btn-link dropdown-toggle dropdown-toggle-split mr-2 m-0 p-0" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                            <span class="icon icon-sm">
                                <span class="fas fa-ellipsis-h icon-secondary fa-lg"></span>
                            </span>
                            <span class="sr-only">Toggle Dropdown</span>
                        </button>
                        <div class="dropdown-menu">
                            <a href="./my-items.html" class="list-group-item list-group-item-action d-sm-none border-0 ">My Items</a>
                            <a href="./security.html" class="list-group-item list-group-item-action d-md-none border-0 ">Security</a>
                            <a href="./billing.html" class="list-group-item list-group-item-action border-0 ">Billing</a>
                            <a href="./messages.html" class="list-group-item list-group-item-action border-0 ">Messages</a> 
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

                    <div class="col-12 col-lg-8 mb-5">
                        <div class="row">
                            <div class="col-lg-12">
                                <div class="card card-body bg-white border-light mb-4">
                                    <h2 class="h5 mb-4">General information</h2>
                                    <form  action="" method="POST" enctype="multipart/form-data">
                                        {% csrf_token %}
                                        <div>
                                            <label for="profile_image">Profile Picture</label>
                                            <input id="profile_image" name="img" style="border-style:none;" type="file" class="form-control">
                                            {{profile_form.profile_image}}
                                        </div><br>
                                        <div class="row">
                                            <div class="col-md-6 mb-3">
                                                <div class="form-group">
                                                    <label for="first_name">First Name</label>
                                                    <input class="form-control" name="first_name" id="first_name" type="text" placeholder="First Name">
                                                    {{profile_form.first_name}}
                                                </div>
                                            </div>
                                            <div class="col-md-6 mb-3">
                                                <div class="form-group">
                                                    <label for="last_name">Last Name</label>
                                                    <input class="form-control" name="last_name" id="last_name" type="text" placeholder="Last Name">
                                                    {{profile_form.last_name}}
                                                </div>
                                            </div>
                                        </div>
                                        <div class="row align-items-center">
                                            <div class="col-md-6 mb-3">
                                                <div class="form-group">
                                                    <label for="birthday">Birthday</label>
                                                    <input type="text" name="birthday" class="form-control flatpickr-input" id="birthday" data-toggle="date" placeholder="Select your birth date">
                                                    {{profile_form.date_of_birth}}
                                                    </div>
                                            </div>
                                            <div class="col-md-6 mb-3 col-lg-9">
                                                <div class="form-group">
                                                    <label for="gender">Gender</label>
                                                    <select class="custom-select" name="gender" id="gender" required>
                                                        
                                                        <option value="Female">Female</option>
                                                    
                                                        
                                                    </select>
                                                </div>
                                            </div>
                                        </div>
                                        <div class="row">
                                            <div class="col-md-6 mb-3">
                                                <div class="form-group">
                                                    <label for="email">Email</label>
                                                    <input class="form-control" name="email" id="email" type="email" placeholder="Email">
                                                    {{user_form.email}}
                                                </div>
                                            </div>
                                            <div class="col-md-6 mb-3">
                                                <div class="form-group">
                                                    <label for="phone">Phone</label>
                                                    <input class="form-control" name="phone" id="phone" type="text" placeholder="Phone">
                                                    {{profile_form.phone}}
                                                    
                                                </div>
                                            </div>
                                        </div>
                                        <h2 class="h5 my-4">Address</h2>
                                        <div class="row">
                                            <div class="col-sm-9 mb-3">
                                                <div class="form-group">
                                                    <label for="address">Address</label>
                                                    <input class="form-control" name="address" id="address" type="text" placeholder="Your address">
                                                    {{profile_form.address}}
                                                </div>
                                            </div>
                                            <!-- <div class="col-sm-3 mb-3">
                                                <div class="form-group">
                                                    <label for="number">Number</label>
                                                    <input class="form-control" name="number" id="number" type="number" placeholder="No.">
                                                </div>
                                            </div> -->
                                        </div>
                                        <div class="row">
                                            <!-- <div class="col-sm-4 mb-3">
                                                <div class="form-group">
                                                    <label for="city">City</label>
                                                    <input class="form-control" name="city" id="city" type="text" placeholder="City">
                                                </div>
                                            </div> -->
                                            <!-- <div class="col-sm-4 mb-3">
                                                <div class="form-group">
                                                    <label for="country">Country</label>
                                                    <select name="country" class="form-control select2-hidden-accessible" id="country" data-toggle="select" title="Country" data-live-search="true" data-live-search-placeholder="Country" data-select2-id="1" tabindex="-1" aria-hidden="true">
                                                        <option value="United Stated">United Stated</option>
                                                        <option value="Canada">Canada</option>
                                                        <option value="Germany">Germany</option>
                                                        <option value="Spain">Spain</option>
                                                        <option value="Italy">Italy</option>
                                                        <option value="UK">UK</option>
                                                    </select>
                                                </div>
                                            </div> -->
                                            <!-- <div class="col-sm-4">
                                                <div class="form-group">
                                                    <label for="zip">ZIP</label>
                                                    <input class="form-control" name="zip" id="zip" type="tel" placeholder="ZIP">
                                                </div>
                                            </div> -->
                                        </div>
                                        <div class="mt-3">
                                            <button type="submit" class="btn btn-primary">Save All</button>
                                            
                                        </div>
                                    </form>
                                </div>
                                <!-- <div class="card card-body bg-white border-light">
                                    <h2 class="h5 mb-4">Alerts & Notifications</h2>
                                    <ul class="list-group list-group-flush">
                                        <li class="list-group-item d-flex justify-content-between pl-0 border-bottom">
                                            <div>
                                                <h3 class="h6 mb-1">Company News</h3>
                                                <span class="small">Get Spaces news, announcements, and product updates</span>
                                            </div>
                                            <div>
                                                <div class="custom-control custom-switch"><input type="checkbox" class="custom-control-input" id="shop-notification-1" checked="checked"> <label class="custom-control-label" for="shop-notification-1"></label></div>
                                            </div>
                                        </li>
                                        <li class="list-group-item d-flex justify-content-between pl-0 border-bottom">
                                            <div>
                                                <h3 class="h6 mb-1">Account Activity</h3>
                                                <span class="small">Get important notifications about you or activity you've missed</span>
                                            </div>
                                            <div>
                                                <div class="custom-control custom-switch"><input type="checkbox" class="custom-control-input" id="shop-notification-2"> <label class="custom-control-label" for="shop-notification-2"></label></div>
                                            </div>
                                        </li>
                                        <li class="list-group-item d-flex justify-content-between pl-0">
                                            <div>
                                                <h3 class="h6 mb-1">Meetups Near You</h3>
                                                <span class="small">Get an email when a Dribbble Meetup is posted close to my location</span>
                                            </div>
                                            <div>
                                                <div class="custom-control custom-switch"><input type="checkbox" class="custom-control-input" id="shop-notification-3" checked="checked"> <label class="custom-control-label" for="shop-notification-3"></label></div>
                                            </div>
                                        </li>
                                    </ul>
                                 </div> -->
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </main>
{% endblock %}

标签: pythondjango-modelsdjango-viewsdjango-forms

解决方案


我重新设计了models.py,forms.py文件如下:-

models.py # Profile models have all field (contains Buyers and Sellers)

class Profile(models.Model):
    CATEGORY_CHOICES = (
        ('M', 'Male'),
        ('F', 'Female'),
    )
    user = models.OneToOneField(User, 
           on_delete=models.CASCADE)
    
    # is_seller field divide the user from Seller and Buyer

    is_seller = models.BooleanField(default=False)

    # if 'is_seller' field false then it means user is 'buyer' and vice-versa

    
    profile_image = models.ImageField(upload_to='photos/', blank=True, null=True)
    date_of_birth =  models.DateField()
    gender = models.CharField(max_length=200, choices=CATEGORY_CHOICES, blank=True, null=True)
    phone = models.CharField(max_length=200, blank=True, null=True)
    address = models.CharField(max_length=200, blank=True, null=True)
    # city = models.CharField(max_length=200, blank=True, null=True)
    # number = models.CharField(max_length=200, blank=True, null=True)
    # zip = models.CharField(max_length=200, blank=True, null=True)
    # country = models.CharField(max_length=200, blank=True, null=True)
    company_name = models.CharField(max_length=100, blank=True, null=True)
    website = models.CharField(max_length=100, blank=True, null=True)
    location = models.CharField(max_length=30, blank=True, null=True)
    bio = models.CharField(max_length=30, blank=True)

现在在 forms.py 文件中根据用户添加必填字段(即用户是买方或卖方):-

forms.py

class BuyersForm(forms.ModelForm):

    class Meta:
        model = Profile
        fields = [# Choose and add fields for buyers here]

class SellerForm(forms.ModelForm):
    class Meta:
        model = Profile
        fields = [# Choose and add fields for seller here]

现在,在前端编写用于登录的自定义注册页面(即名字,姓氏,用户名,电子邮件,密码),并且必须有一个选择器(选择卖方或买方)。当用户点击提交时,然后保存所有值to User(即名字、姓氏、用户名、电子邮件、密码)以及 in Profile(即 is_seller 为 True 或 False 并添加 User 实例)。之后(假设用户选择sellerthen)发送SellerForm前端并要求用户填写所需填写的内容。

但是如果您想了解有关代理模型的更多信息,还有另一种方法(即使用proxy模型),请转到此链接:-单击此处


推荐阅读