secret
Created

  • Download Gist
gistfile1.pyw
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
 
def create_preview_model(cls):
"Create a preview model for a PreviewModel"
name = cls.__name__ + "Preview"
 
#Set up a dictionary to simulate declarations within a class
attrs = {
'__module__': cls.__module__,
}
#OneToOne to the model we are previewing
attrs['preview_parent'] = models.OneToOneField(cls, related_name='preview')
 
attrs['__str__'] = preview_string
 
#duplicate all fields into this class, set your dependencies if any
cls.dependencies = []
for field in cls._meta.fields:
if field.name != "preview_state":
if isinstance(field, (ForeignKey, OneToOneField, ManyToManyField)):
field_class = type(field)
rel = field.rel
field_dict = { "unique": field.unique,
"null": field.null,
"blank": field.blank
}
if rel.related_name:
field_dict["related_name"] = "preview" + rel.related_name
 
attrs[field.name] = field_class(rel.to, **field_dict)
 
#if the dependency is a PreviewModel we will notify it of the model's current status
dependency_fields = [f.name for f in rel.to._meta.fields]
is_required = not field.blank and (not field.null or isinstance(field, ManyToManyField))
if "preview_state" in dependency_fields and is_required:
cls.dependencies.append(field.name)
else:
attrs[field.name] = field
 
#create the new class and set it on the PreviewModel class for later access
new_class = type(name, (models.Model,), attrs)
cls.preview_class = new_class
return new_class

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.