I have models with GenricForeigKey and GenericRelation fields.
class Datasheet(models.Model):
package1 = GenericRelation('PackageInstance')
...
class PackageInstance(models.Model):
content_object = GenericForeignKey()
object_id = models.PositiveIntegerField(null=True)
content_type = models.ForeignKey(ContentType, null=True, on_delete=models.CASCADE)
....
I am migrating from another models, inside my migration I want to create new instance.
for ds in Datasheet.objects.all():
pi = PackageInstance.objects.create(content_object=ds)
However this fails
TypeError: DesignInstance() got an unexpected keyword argument 'content_object'
Additionally, ds.package1.all() will also fail.
AttributeError: 'Datasheet' object has no attribute 'package1'
How do I solve this?
I did some research but did not find a direct answer to my question. The most important thing to remember is that model methods will not be available in migrations. This includes fields created by the Content Types framework. However,
object_idandcontent_typewill be there.My solution is to simply create things by hand.