在Django中创建动态模型字段?
在我的型号中,我的模型商店包含名称,
现在是brand_name字段,我想从Django Admin动态创建名为brand_type的新字段,该字段如何,我该如何执行此操作? 我们可以使用模型架构更改模型模型吗?如何?
In my models.py my model store contains name, brand_name fields
Now,I want to create new field called brand_type in store model dynamically from Django admin,how can I do this?
Can we change schema of model using SchemaEditor? How?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您无法通过管理面板创建模型字段,
您
必须
在型号中创建字段
。
you can't create model's fields via admin panel
you
must
create fields inmodels.py
admin panel is only a convinient way to manipulate your database, but not modifiyng its tables
服务器运行时需要迁移。创建
brand_type
在模型下的字段,并设置空白= trueIt requires migration while the server is running. Create
brand_type
field under your model and set blank=True在您的
中
models.py.py
-types“ rel =” nofollow noreferrer”>文本选择
或者
如果您想根据用例,请更 dynamic ,
创建一个称为brandType的模型,然后将其链接在
store
使用brand_type = models.foreignkey(brandType,on_delete = models.protect)
models.py
< <代码> admin.py
带走:不建议使用admin直接修改
models.py
文件,这将导致完整性问题,并且有更好的方法可以实现您所需的功能。In your
models.py
In your console
Extra Stuff:
If you are interested in Text Choices
or
If you wanna make it more dynamic based on your use case,
create a model called BrandType and link it in
Store
usingbrand_type = models.ForeignKey(BrandType,on_delete=models.PROTECT)
models.py
admin.py
Take away: It is not advisable to modify your
models.py
file using admin directly it will cause integrity issues and there are better ways to achieve your desired functionality.