好吧,新手。因此,我正在做一个项目,其中的要求是:我必须制作一个客户模型,管理员/员工可以添加客户的详细信息。 现在的问题是管理员/员工可以针对一个客户添加多个地址。我找不到任何正确的想法,我该怎么做呢。 但我还是制定了一个计划。。在
我加了一些代码。在
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
^{pr2}$
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
这里有几个地方不对劲。在
模型名是标题大小写的
Customer
,而不是customer
。在不知道为什么你要使用两个应用程序来处理
customer
和customer_address
图像字段} ,因为这有图像的验证逻辑。在
profile_image
应该使用^{一个将建议不要在模型上有方法返回它应该在模板中的图像的标记。在
像
<img src="{{ MEDIA_URL }}{{ image.imgfile.name }}"></img>
将导入放入以下组:
future
,standard library
,third-party libraries
,其他Django组件,local Django component
,try/excepts
看看django coding style
带默认值的外键您应该确保在迁移之前创建了对象。看看 Setting default value for Foreign Key attribute
同时澄清你的问题。在
相关问题 更多 >
编程相关推荐