我有以下设置,其中我的模型类看起来像
from django.db import models
from User.models import UserProfile
class Advertisement(models.Model):
owner = models.ForeignKey(UserProfile, related_name='advertisements', on_delete=models.CASCADE)
title = models.CharField(max_length=500, blank=False, default='')
location = models.CharField(max_length=300, blank=False, default='')
rent = models.IntegerField(blank=False)
status = models.CharField(max_length=100, blank=False)
no_of_bedrooms = models.IntegerField(blank=False)
no_of_bathrooms = models.IntegerField(blank=False)
posted_by = models.CharField(max_length=100, blank=True)
date_ad_posted = models.DateTimeField(null=True, blank=True)
contact = models.CharField(max_length=13, blank=False)
description = models.CharField(max_length=1000, blank=False)
security_deposit = models.IntegerField(default=0, blank=False)
def save(self, *args, **kwargs):
super(Advertisement, self).save(*args, **kwargs)
class File(models.Model):
Ad = models.ForeignKey(Advertisement, related_name='ad-photo', on_delete=models.CASCADE)
file = models.FileField(blank=False, null=False)
remark = models.CharField(max_length=20)
timestamp = models.DateTimeField(auto_now_add=True)
我的序列化工具如下所示
class AdvertisementSerializer(serializers.HyperlinkedModelSerializer):
owner = serializers.ReadOnlyField(source='owner.email')
class Meta:
model = Advertisement
fields = ('id', 'owner', 'title', 'location', 'rent', 'status', 'no_of_bedrooms', 'no_of_bathrooms',
'date_ad_posted', 'posted_by', 'contact', 'description')
kwargs = {
'url': {'view_name': 'advertisement-detail'}
}
class FileSerializer(serializers.ModelSerializer):
class Meta:
model = File
fields = ('Ad', 'file', 'remark', 'timestamp')
我正在以下列方式在admin.py中注册我的模型
from django.contrib import admin
from .models import Advertisement, AdContract, File
class FileInline(admin.TabularInline):
model = File
class PropertyAdmin(admin.ModelAdmin):
inlines = [FileInline, ]
admin.site.register(Advertisement, File)
admin.site.register(AdContract)
我尝试了谷歌搜索后得到的一些解决方案,但错误仍然存在。任何帮助表示赞赏。
答案 0 :(得分:2)
要一次注册多个模型,您需要将它们作为可迭代项传递:
admin.site.register([Advertisement, File])
而不是
admin.site.register(Advertisement, File)
答案 1 :(得分:1)
vector (Vector (XY xy1) (XY xy2)) = [xy1, xy2]
用于注册带有管理类的模型类。
这是它的功能签名:
admin.site.register
在您AdminSite.register(model_or_iterable, admin_class=None, **options)
中,您传递的是文件模型,而不是自定义管理员类:admin.py
试试这个:
admin.site.register(Advertisement, File)
答案 2 :(得分:0)
使用admin.site.register(Advertisement, FileInline)
代替admin.site.register(Advertisement, File)
答案 3 :(得分:0)
我遇到了类似的问题。像您一样,我正在注册Model
,而不是admin
模型。
尝试一下:
admin.site.register(File, FileInLine)