帮助为 Django“项目”设计模型应用程序中包含 1 个或多个 url(变量),可在管理界面中立即编辑

发布于 2024-08-20 06:23:54 字数 873 浏览 13 评论 0原文

我正在为 Django 构建一个简单的应用程序,但我的模型设计以及管理界面中“内联”的使用面临一些问题。

该应用程序用于管理项目。
一个项目由名称、描述、其他字段(例如标签...)和多个url(如项目url、源代码存储库url)组成,但数量不是固定的,它们可以有 1、2 或更多 url(我认为 0 永远不会出现这种情况)

我首先创建了简单的模型,例如:

class Url(models.Model):
    name = models.CharField(max_length=100)
    url = models.URLField()


class Project(models.Model):
    name = models.CharField(max_length=100)
    description = models.CharField(max_length=300)
      ...
    urls = models.ManyToManyField(Url, blank=True)

但是使用这些模型,我无法拥有一个管理界面,可以在其中创建一个项目并同时添加一个或多个网址。
我尝试在管理网站中使用“内联”,如文档中所示,但没有成功。

我什至不确定模型/数据库设计(例如,网址不会在各种项目中重用,并且manytomanyfield允许您在可能没有必要的所有现有网址之间进行选择),但我不知道不知道还有什么其他解决方案(列表,...)

谁能帮我解决这个(我认为很简单)问题?
请告诉我一些选择模型/数据库设计的有用指南?
甚至可能向我指出一些实现此类问题的示例代码,并带有所示的管理界面?

感谢您的回复,如果不够清楚,请随时询问详细信息。

i'm building a simple app for Django and I'm facing some problems with my models design, and the use of 'inlines' in the administration interface.

The app is to manage projects.
A project consists of a name, a description, other fields (e.g. tags...) and of multiple urls (like project url, source code repository url) but the number is not fixed, they can have 1, 2 or more urls (i think 0 will never be the case).

I have first created simple models like:

class Url(models.Model):
    name = models.CharField(max_length=100)
    url = models.URLField()


class Project(models.Model):
    name = models.CharField(max_length=100)
    description = models.CharField(max_length=300)
      ...
    urls = models.ManyToManyField(Url, blank=True)

but with these models I did't manage to have an admin interface where i can had a project and add at the same time one or more urls.
I have tried to use 'inlines' in the admin website like indicated in the doc but without success.

I'm not even sure the models/database design (e.g. urls won't be reused in various projects, and the manytomanyfield let you choose between allready existing urls which may not be necessary), but I don't know what can be other solutions (lists, ...).

Can anyone help me with this (simple i think) problem?
Indicate me some useful guidelines to choose a models/db design?
Even maybe point me to some example code implementing this sort of problem, with admin interface as indicated?

Thanks for responses and don't hesitate to ask details if it's not clear enough.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一人独醉 2024-08-27 06:23:55

我想我已经找到了解决问题的开始。
将项目中的ManyToManyField更改为Url中的ForeignKey,并创建如下所示的内联

# models.py
class Project(models.Model):
  name = models.CharField(max_length=100)
  description = models.CharField(max_length=300)
  ...

class Url(models.Model):
  name = models.CharField(max_length=100)
  url = models.URLField()
  project = models.ForeignKey(Project)

# admin.py
class UrlInline(admin.TabularInline):
  model = Url

class ProjectAdmin(admin.ModelAdmin):
  inlines = [ UrlInline, ]

admin.site.register(Project,ProjectAdmin)

现在我可以添加特定于项目的url,编辑它们,甚至删除它们:)
而且我没有看到其他项目中使用的网址。

我认为它可以开始工作,并且,作为信息,我在右侧栏上找到了一些想法,查看另一篇文章 Django 管理 - 内联内联(或者,一次编辑三个模型) :)

I think I have found the beginning of a solution to my problem.
Changing the ManyToManyField in Project to a ForeignKey in Url, and creating inlines like indicated below

# models.py
class Project(models.Model):
  name = models.CharField(max_length=100)
  description = models.CharField(max_length=300)
  ...

class Url(models.Model):
  name = models.CharField(max_length=100)
  url = models.URLField()
  project = models.ForeignKey(Project)

# admin.py
class UrlInline(admin.TabularInline):
  model = Url

class ProjectAdmin(admin.ModelAdmin):
  inlines = [ UrlInline, ]

admin.site.register(Project,ProjectAdmin)

Now i can add urls specific to a project, edit them, even delete them :)
And I don't see urls used in other projects.

I think it does the job to begin, and, for information, I found ideas on the right sidebar looking at this other post Django admin - inline inlines (or, three model editing at once) :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文