私はマルチテナントアプリケーションで作業しています。このアプリケーションでは、フォーム内に追加データを収集してデータを報告するための独自のデータフィールドを管理者が定義できます。後者のビットがJSONFieldない偉大なオプションになりますので、代わりに私は、次の解決策がありますCustomDataFieldはサイトへのForeignKeyを持っているかDjangoの動的モデルフィールド
class CustomDataField(models.Model):
"""
Abstract specification for arbitrary data fields.
Not used for holding data itself, but metadata about the fields.
"""
site = models.ForeignKey(Site, default=settings.SITE_ID)
name = models.CharField(max_length=64)
class Meta:
abstract = True
class CustomDataValue(models.Model):
"""
Abstract specification for arbitrary data.
"""
value = models.CharField(max_length=1024)
class Meta:
abstract = True
注 - 各サイトは、カスタムデータフィールドの異なるセットを持っていますが、同じを使用します。データベース。 その後、様々な具体的なデータフィールドは次のように定義できます。これは、次の使用につながる
class UserCustomDataField(CustomDataField):
pass
class UserCustomDataValue(CustomDataValue):
custom_field = models.ForeignKey(UserCustomDataField)
user = models.ForeignKey(User, related_name='custom_data')
class Meta:
unique_together=(('user','custom_field'),)
:
custom_field = UserCustomDataField.objects.create(name='zodiac', site=my_site) #probably created in the admin
user = User.objects.create(username='foo')
user_sign = UserCustomDataValue(custom_field=custom_field, user=user, data='Libra')
user.custom_data.add(user_sign) #actually, what does this even do?
しかし、これは特に、手動で関連するデータを作成するために必要で、非常に不格好な感じとそれを具体的なモデルに関連付けます。より良いアプローチがありますか?前emptively破棄された
オプション:
- カスタムSQLオンザフライテーブルを変更します。部分的には、これは拡張性がなく、部分的にはハックが大きすぎるためです。
- NoSQLのようなスキーマレスソリューション。私は彼らに対して何もしていないが、彼らはまだよく適合していない。最終的にこのデータはと入力されており、第三者報告アプリケーションを使用する可能性があります。
- 上記のJSONFieldは、クエリでうまく機能しないため、今日のよう
プリemptively、これはこれらの質問のいずれかではありません。 http://stackoverflow.com/questions/7801729/django-model-with-dynamic-attributes http://stackoverflow.com/questions/ 2854656/per-instance-dynamic-fields-django-model – GDorn