ManyToMany字段在管理屏幕中断,不应该



我有一些这样的模型:

class Category(models.Model):
class Meta:
ordering = ['name']
name = models.CharField(max_length=100)
text = models.TextField(blank=True)
def __str__(self):
return self.name
class Tag(models.Model):
name = models.CharField(max_length=100)
def __str__(self):
return self.name
class Tool(models.Model):
name = models.CharField(max_length=30, null=True, default='')   
url = models.URLField(max_length=250, null=True, default='')
image_url = models.URLField(max_length=250, null=True, default='', blank=True)
about = models.TextField(default='', null=True, blank=True)
tags = models.ManyToManyField( Tag, related_name="tools" , blank=True)
category = models.ForeignKey(Category, on_delete=models.CASCADE, blank=True,null=True, related_name="category1")
altcategory = models.ForeignKey(Category, on_delete=models.CASCADE, blank=True,null=True, related_name="category2")

一切似乎都很好,除了当我在Admin屏幕中为Tool添加标签时。我可以创建一个工具和标签,但当我在Admin屏幕的标签列表中选择一个标签并保存时,我会得到:

The above exception (syntax error at or near "ON" LINE 1: ...ls_tool_tags" ("tool_id", "tag_id") VALUES (1, 2) ON CONFLIC... ^ ) was the direct cause of the following exception:

使用sql:


('INSERT INTO "tools_tool_tags" ("tool_id", "tag_id") VALUES (%s, %s) ON '
'CONFLICT DO NOTHING')

DEBUG屏幕显示错误位于"tag_id",这很奇怪。。。

我希望这不是一个版本的问题,因为我正在使用Heroku,并且对一切"正常工作"的方式印象深刻。我的Django版本是3.0.4和Postgres12.2。

我不认为ManyToMany是开箱即用的。所以我不得不创建一个"直通"模型。。。并将管理表单更改为。。。

型号.py

from django.db import models
from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation
from django.contrib.auth.models import User
from django.utils.html import format_html
from django.contrib.contenttypes.models import ContentType
class Category(models.Model):
class Meta:
ordering = ['name']
name = models.CharField(max_length=100)
text = models.TextField(blank=True)
def __str__(self):
return self.name

class Tag(models.Model):
name = models.CharField(max_length=100)
def __str__(self):
return self.name
class Tool(models.Model):
name = models.CharField(max_length=30, null=True, default='')   
url = models.URLField(max_length=250, null=True, default='')
image_url = models.URLField(max_length=250, null=True, default='', blank=True)
about = models.TextField(default='', null=True, blank=True)
tags = models.ManyToManyField( 
Tag, 
through="PageTags",
through_fields=('tool', 'tag'))
category = models.ForeignKey(Category, on_delete=models.CASCADE, blank=True,null=True, related_name="category1")
altcategory = models.ForeignKey(Category, on_delete=models.CASCADE, blank=True,null=True, related_name="category2")
def _get_thumbnail(self):
return format_html(u'<img src="{}" width="150"/>', self.image_url)
_get_thumbnail.allow_tags = True
def _get_link(self):
return format_html(u'<a href="{}" target="_blank"/>{}</a>', self.url, self.url)
_get_link.allow_tags = True
def __str__(self):
return self.name + ": " + self.url
class PageTags(models.Model):
tag = models.ForeignKey(Tag ,   on_delete=models.CASCADE, )
tool= models.ForeignKey(Tool ,  on_delete=models.CASCADE, )
amount = models.IntegerField(default=0)

并对此进行管理。。。

from django.contrib import admin
from django.contrib.admin import TabularInline 
#from django.contrib.contenttypes.admin import  GenericTabularInline
from django.utils.html import format_html_join
from django.utils.safestring import mark_safe
from .models import Tool, Category, Tag, PageTags

class PageTagsInline(admin.TabularInline): 
model = PageTags
extra = 1

class ToolAdmin(admin.ModelAdmin):
#fields = ('name', )
list_display = ('name','_get_link','category','altcategory', "_get_thumbnail", )
list_filter = ('category',)
search_fields = ['name', "about"]
filter_horizontal = ('tags',)
inlines = [PageTagsInline,]

admin.site.register(Tag)
admin.site.register(Category)
admin.site.register(Tool, ToolAdmin)

界面也不同,不像权限小部件,但还好……至少不会轰炸管理员。

相关内容

  • 没有找到相关文章

最新更新