django-dynamic-models
django-dynamic-models copied to clipboard
One to Many, Many to Many
Thoughts on supporting Foreign Key field and many to many fields?
Those features would be quite nice, and I would love to see them implemented! I'm having a hard time finding time to dedicate to this project at the moment though. PRs are welcome!
ok. So sounds like conceptually this is doable within the current framework of django-dynamic-models.
Is the right way to do this by adding a new DATA_TYPE in FieldFactory and the associated implementation?
Yea, it should be doable. There are other considerations that need to be taken into account for these fields though compared to the simpler data types.
@virtualbrown any progress on implementing Foreign Key? Is adding a new DATA_TYPE right way to do this?
@macieyn
any progress on implementing Foreign Key? Is adding a new DATA_TYPE right way to do this?
0.2.0 was finally released today., and this feature unfortunately didn't make it in, but it is definitely on the roadmap. Check out the new API, and PRs are always welcome 😄
Hi @rvinzent
I want to work on this feature. Would you please tell me what are the considerations that need to be taken into account for these fields?
HI @adavoudi thanks for your interest! I have been struggling to find the time to implement some of these features.
There are a few things that might make this a bit challenging given the current architecture. The kwargs for generated Field classes are currently each stored as DB column on FieldSchema. ManyToMany and ForeignKey each have a special set of kwargs that only apply to those fields, so I don't think it would be very scalable or easy to implement if we simply added more fields to the model.
I think there is a more proper refactoring we can do that should allow any field class (including custom fields) to be used. Instead of storing the data_type on the FieldSchema model and mapping this to a Field class, we should instead change this to be qualified class name of the field class to use. Then, instead of listing out each kwarg as a separate model field, we can replace all of that with a JSONField that can hold any necessary kwargs to construct the field.
The new FieldSchema should look something like this:
class FieldSchema(models.Model):
name = models.CharField(max_length=63)
model_schema = models.ForeignKey(ModelSchema, on_delete=models.CASCADE, related_name="fields")
class_name = models.TextField()
kwargs = models.JSONField(default=dict)
The new factory implementation should first import the field class by passing class_name to importlib, and then construct the field instance like field_class(**field_schema.kwargs).
Let me know if you have any questions, and if you're not up for the task I will do my best to prioritize this :)
@rvinzent Great tips. I will work on this and ask for your guides if needed.
Can you please explain how can i use new field schema, because documentation is not updated yet