你将如何继承并覆盖django模型类来创建一个listOfStringsField? [英] How would you inherit from and override the django model classes to create a listOfStringsField?

查看:124
本文介绍了你将如何继承并覆盖django模型类来创建一个listOfStringsField?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为基本上是ListOfStrings的django模型创建一个新的类型的字段。所以在您的模型代码中,您将具有以下内容:



models.py:

  from django.db import models 

class ListOfStringsField(???):
???

class myDjangoModelClass():
myName = models.CharField(max_length = 64)
myFriends = ListOfStringsField()#
pre>

other.py:

  myclass = myDjangoModelClass()
myclass.myName =bob
myclass.myFriends = [me,myself,and I]

myclass.save ()

id = myclass.id

loadedmyclass = myDjangoModelClass.objects.filter(id__exact = id)

myFriendsList = loadedclass.myFriends
#myFriendsList是一个列表,应该等于[我,我自己,和我]

您如何按照以下规定来编写此字段类型?




  • 我们不想创建一个字段它们将所有的字符串汇集在一起​​,并将它们与一个字段中的令牌分开,如这个。在某些情况下,这是一个很好的解决方案,但是我们希望保持字符串数据的规范化,除了django之外的工具可以查询数据。

  • 该字段应该自动创建任何需要存储的辅助表字符串数据。

  • 次表最好只有每个唯一字符串的一个副本。这是可选的,但很高兴。



查看Django代码,看起来像我想做一些类似于外国人正在做什么,但文件稀少。



这将导致以下问题:




  • 可以这样做吗?

  • 是否已经完成(如果是这样)?

  • Django有没有关于如何扩展和覆盖他们的模型类,特别是他们的关系类?我没有看到很多关于他们代码的方面的文档,但是有一个这个



这是来自这个question

解决方案

您所描述的内容对我来说非常类似于标签。

那么为什么不使用 django标记

它的作用就像一个魅力,你可以独立安装从您的应用程序及其API很容易使用。


I want to create a new type of field for django models that is basically a ListOfStrings. So in your model code you would have the following:

models.py:

from django.db import models

class ListOfStringsField(???):
    ???

class myDjangoModelClass():
    myName = models.CharField(max_length=64)
    myFriends = ListOfStringsField() # 

other.py:

myclass = myDjangoModelClass()
myclass.myName = "bob"
myclass.myFriends = ["me", "myself", "and I"]

myclass.save()

id = myclass.id

loadedmyclass = myDjangoModelClass.objects.filter(id__exact=id)

myFriendsList = loadedclass.myFriends
# myFriendsList is a list and should equal ["me", "myself", "and I"]

How would you go about writing this field type, with the following stipulations?

  • We don't want to do create a field which just crams all the strings together and separates them with a token in one field like this. It is a good solution in some cases, but we want to keep the string data normalized so tools other than django can query the data.
  • The field should automatically create any secondary tables needed to store the string data.
  • The secondary table should ideally have only one copy of each unique string. This is optional, but would be nice to have.

Looking in the Django code it looks like I would want to do something similar to what ForeignKey is doing, but the documentation is sparse.

This leads to the following questions:

  • Can this be done?
  • Has it been done (and if so where)?
  • Is there any documentation on Django about how to extend and override their model classes, specifically their relationship classes? I have not seen a lot of documentation on that aspect of their code, but there is this.

This is comes from this question.

解决方案

What you have described sounds to me really similar to the tags.
So, why not using django tagging?
It works like a charm, you can install it independently from your application and its API is quite easy to use.

这篇关于你将如何继承并覆盖django模型类来创建一个listOfStringsField?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆