如何从Django admin添加多个客户地址

2024-04-19 02:27:33 发布

您现在位置:Python中文网/ 问答频道 /正文

好吧,新手。因此,我正在做一个项目,其中的要求是:我必须制作一个客户模型,管理员/员工可以添加客户的详细信息。 现在的问题是管理员/员工可以针对一个客户添加多个地址。我找不到任何正确的想法,我该怎么做呢。 但我还是制定了一个计划。。在

enter image description here

我加了一些代码。在

客户-模型.py在

from __future__ import unicode_literals
from django.contrib.postgres.fields import JSONField
from decimal import Decimal
from django.db import models
from django.utils.safestring import mark_safe

# Create your models here.
class customer(models.Model):
    first_name = models.CharField(max_length=150)
    last_name = models.CharField(max_length=150)
    profile_image = models.FileField()
    phone_number = models.BigIntegerField()

    def profile(self):
        if self.profile_image!="":
            return mark_safe('<img src="/media/%s" width="150" height="150" />' % (self.profile_image))
        else:
            return "";
    profile.short_description = 'Image'
    # profile.allow_tags = True

客户地址-模型.py在

^{pr2}$

客户-管理员py在

from __future__ import unicode_literals
from .models import customer
from customer_address.models import Address
from django.contrib import admin

# Register your models here.
class CustomerAddressInline(admin.StackedInline):
    model = Address

class CustomerAdmin(admin.ModelAdmin):
    list_display = ["first_name","last_name","phone_number","profile"]
    list_select_related = True
    inlines = [
        CustomerAddressInline,
    ]

    search_fields = ["first_name","last_name","phone_number"]
    fields = ( "first_name","last_name","phone_number",'profile',"profile_image","customer_address" )
    readonly_fields = ('profile',)
    class Meta:
        model = customer

admin.site.register(customer,CustomerAdmin)

不明白,我如何显示/编辑/插入所有字段作为地址在客户模块从管理员。一点帮助将不胜感激 谢谢你 P、 S:我用的是django-1.11.10


Tags: djangonamefromimageimportfields客户models
1条回答
网友
1楼 · 发布于 2024-04-19 02:27:33

这里有几个地方不对劲。在

模型名是标题大小写的Customer,而不是customer。在

不知道为什么你要使用两个应用程序来处理customercustomer_address

图像字段profile_image应该使用^{},因为这有图像的验证逻辑。在

一个将建议不要在模型上有方法返回它应该在模板中的图像的标记。在

<img src="{{ MEDIA_URL }}{{ image.imgfile.name }}"></img>

将导入放入以下组:futurestandard librarythird-party libraries,其他Django组件,local Django componenttry/excepts

看看django coding style

带默认值的外键您应该确保在迁移之前创建了对象。看看 Setting default value for Foreign Key attribute

from __future__ import unicode_literals

from django.contrib import admin

from .models import Customer, Address

class CustomerAddressInline(admin.StackedInline):
    model = Address

@admin.register(Customer)
class CustomerAdmin(admin.ModelAdmin):
    list_display = ["first_name","last_name","phone_number"]
    inlines = [CustomerAddressInline]

    search_fields = ["first_name","last_name","phone_number"]
    fields = ("first_name","last_name","phone_number", "profile_image",)

同时澄清你的问题。在

相关问题 更多 >