我已经检查了这个,但是我需要在两个字段中连接内容,这是问题所在。
我有两个型号,brand
而product
这看起来是这样的:
品牌型号
class Brand(models.Model):
name = models.CharField(max_length=29)
website = models.URLField()
def __unicode__(self):
return self.name
产品型号
class Product(models.Model):
brand = models.ForeignKey(Brand)
name = models.CharField(max_length=140)
slug = models.SlugField()
def __unicode__(self):
return self.name
管理方法
from .models import Product
@admin.register(Product)
class ProductAdmin(admin.ModelAdmin):
prepopulated_fields = {'slug': ('name',)}
模型上的样本输入:
**Brand**
Name: Example
Website: http://www.example.com
**Product**
Brand: Example (Selection)
Name: Product
Slug: product(prepopulated)
我希望那子弹example-product
比product
。我怎样才能将品牌和名称都塞进去。
感谢您的任何帮助。
将品牌添加到prepopulated_fields词典中只会返回ID,而不返回str / unicode值。
class ProductAdmin(admin.ModelAdmin):
prepopulated_fields = {'slug': ('brand', 'name',)}
一种方法是添加save_model方法ModelAdmin
from django.utils.text import slugify
class ProductAdmin(admin.ModelAdmin):
def save_model(self, request, obj, form, change):
# don't overwrite manually set slug
if form.cleaned_data['slug'] == "":
obj.slug = slugify(form.cleaned_data['brand']) + "-" + slugify(form.cleaned_data['name'])
obj.save()
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句