我想定義一組模型/對象,它們可以用來表示關係:field_set有許多字段,其中字段是django.db.model字段對象(IPAddressField,FilePathField等)。如何使用任意數量的任意字段類型對對象進行建模? (django orm)
我的目標是有一個ORM模型,它支持以下類型的'API'。
從控制器視圖讓說:
# Desired api below
def homepage(request):
from mymodels.models import ProfileGroup, FieldSet, Field
group = ProfileGroup()
group.name = 'Profile Information'
group.save()
geographicFieldSet = FieldSet()
# Bind this 'field set' to the 'profile group'
geographicFieldSet.profilegroup = group
address_field = Field()
address_field.name = 'Street Address'
address_field.f = models.CharField(max_length=100)
# Bind this field to the geo field set
address_field.fieldset = geographicFieldSet
town_field = Field()
town_field.name = 'Town/City'
town_field.f = models.CharField(max_length=100)
# Bind this field to the geo field set
town_field.fieldset = geographicFieldSet
demographicFieldSet = FieldSet()
demographicFieldSet.profilegroup = group
age_field = Field()
age_field.name = 'Age'
age_field.f = models.IntegerField()
# Bind this field to the demo field set
age_field.fieldset = demographicFieldSet
# Define a 'weight_field' here similar to 'age' above.
for obj in [geographicFieldSet, town_field, address_field,
demographicFieldSet, age_field, weight_field]:
obj.save()
# Id also add some methods to these model objects so that they
# know how to render themselves on the page...
return render_to_response('page.templ', {'profile_group':group})
基本上我要支持「邏輯分組域」,因爲我看到自己不同類型的支持許多「字段設置」因此我渴望一個有意義的抽象。
我喜歡定義這個模型,以便我可以定義一組字段,其中字段的字段與字段類型一樣是任意的。所以,我可能有一個字段組「地理」,其中包括字段「國家」(CharField W /選擇),「城」(文本字段)等
繼承人伊夫想出迄今:
class ProfileGroup(models.Model):
name = models.CharField(max_length=200)
# FieldSets have many Fields
class FieldSet(models.Model):
name = models.CharField(max_length=200)
profilegroup = models.ForeignKey(ProfileGroup)
class Field(models.Model):
f = models.Field()
fieldset = models.ForeignKey(FieldSet)
儘管使用這些模型會在shell中產生錯誤,並最終不允許我存儲任意字段。
In [1]: from splink.profile_accumulator.models import Field, FieldSet, ProfileGroup
In [2]: import django.db
In [3]: profile_group = ProfileGroup()
In [4]: profile_group.name = 'profile group name'
In [5]: profile_group.save()
In [6]: field_set = FieldSet()
In [7]: field_set.name = 'field set name'
In [8]: field_set.profilegroup = profile_group
In [9]: field_set.save()
In [10]: field = Field()
In [11]: field.name = 'field name'
In [12]: field.f = django.db.models.FileField()
In [13]: field.save()
---------------------------------------------------------------------------
ProgrammingError Traceback (most recent call last)
/var/www/splinkpage.com/splinkpage.pinax/splink/<ipython console> in <module>()
/usr/lib/pymodules/python2.5/django/db/models/base.pyc in save(self, force_insert, force_update)
309 raise ValueError("Cannot force both insert and updating in "
310 "model saving.")
--> 311 self.save_base(force_insert=force_insert, force_update=force_update)
312
313 save.alters_data = True
/usr/lib/pymodules/python2.5/django/db/models/base.pyc in save_base(self, raw, cls, force_insert, force_update)
381 if values:
382 # Create a new record.
--> 383 result = manager._insert(values, return_id=update_pk)
384 else:
385 # Create a new record with defaults for everything.
/usr/lib/pymodules/python2.5/django/db/models/manager.pyc in _insert(self, values, **kwargs)
136
137 def _insert(self, values, **kwargs):
--> 138 return insert_query(self.model, values, **kwargs)
139
140 def _update(self, values, **kwargs):
/usr/lib/pymodules/python2.5/django/db/models/query.pyc in insert_query(model, values, return_id, raw_values)
890 part of the public API.
891 """
892 query = sql.InsertQuery(model, connection)
893 query.insert_values(values, raw_values)
--> 894 return query.execute_sql(return_id)
/usr/lib/pymodules/python2.5/django/db/models/sql/subqueries.pyc in execute_sql(self, return_id)
307
308 def execute_sql(self, return_id=False):
--> 309 cursor = super(InsertQuery, self).execute_sql(None)
310 if return_id:
311 return self.connection.ops.last_insert_id(cursor,
/usr/lib/pymodules/python2.5/django/db/models/sql/query.pyc in execute_sql(self, result_type)
1732
1733 cursor = self.connection.cursor()
-> 1734 cursor.execute(sql, params)
1735
1736 if not result_type:
/usr/lib/pymodules/python2.5/django/db/backends/util.pyc in execute(self, sql, params)
17 start = time()
18 try:
---> 19 return self.cursor.execute(sql, params)
20 finally:
21 stop = time()
ProgrammingError: can't adapt
所以我想知道,如果這是完全錯誤的做法,或者如果我需要使用Django的模型類有點不同得到我想要的東西。
@保羅,如果你讓你的問題更短的也許更多的人會看看吧:) – Evgeny 2009-07-10 17:45:05