Ich möchte eine Reihe von Modell/Objekte definieren, die für eine die Beziehung darstellen können: field_set hat viele Felder, wo Felder django.db.model Feldobjekte (IPAddressField, FilePathField etc.) sind.
Mein Ziel ist es, ein ORM-Modell zu haben, das die folgende Art von "api" unterstützt.
Von einem Controller-Ansicht lässt sagen:
# 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})
Im Wesentlichen möchte ich "logisch gruppierte Felder" unterstützen, da ich mir vorstellen kann, viele "Feldsätze" verschiedener Typen zu unterstützen, daher mein Wunsch nach einer sinnvollen Abstraktion.
Ich möchte dieses Modell so definieren, dass ich eine Gruppe von Feldern definieren kann, wobei die Anzahl der Felder beliebig ist, ebenso wie der Feldtyp. So könnte ich eine Feldgruppe "Geographic" haben, die die Felder "State" (CharField w/ choices), "Town" (TextField) usw. enthält.
Hier ist, was ich bis jetzt gefunden habe:
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)
Allerdings führt die Verwendung dieser Modelle zu einem Fehler in der Shell und erlaubt es mir letztendlich nicht, beliebige Felder zu speichern.
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
So Im wundern, wenn dies völlig der falsche Ansatz ist oder wenn ich django's Modellklassen ein bisschen anders verwenden müssen, um zu erhalten, was ich will.