wq/django-data-wizard

Cannot search Natural Key when use unique=True in a NaturalKeyModel

stanbird08 opened this issue · 3 comments

models.py:
class FloorPlan(NaturalKeyModel):
name = models.CharField(max_length=100, null=False, unique=True)
stories = models.CharField(max_length=10, null=True, default=Single)

class FloorPlanFacade(models.Model):
floorplan_name = models.ForeignKey(FloorPlan, on_delete=models.CASCADE, null=False)
name = models.CharField(max_length=100, null=False)
image = models.ImageField(upload_to='floorplanfacade_images/%Y/%m/%d/')

wizard.py
class FloorPlanFacadeSerializer(NaturalKeyModelSerializer):
class Meta:
model = FloorPlanFacade
fields = "all"

Excel spreadsheet, Facade.xls
floorplan_name, name, image

On Admin -> Data Wizard - Sources -> Import via data wizard,
After mapping columns, I have to manually match floorplan_name column with values in FloorPlan, instead of searching natural key automatically.

However, if I added FloorPlan model with unqiue_together, the foreign key values are automatically matched with columns.

class FloorPlan(NaturalKeyModel):
name = models.CharField(max_length=100, null=False, unique=True)
stories = models.CharField(max_length=10, null=True, default=Single)

class Meta:
    unique_together = (('floorplan_name', 'name'),)

Note that the first time you run the wizard, you will generally need to manually map all foreign keys even if there is an obvious match (see #14). Once you run the wizard a second time, the match will be re-used. This might be why it worked the second time in this case (rather than the model change). Can you try testing the two setups in reverse on a fresh database?

You can avoid the ID mapping step entirely by triggering the /datawizard/[id]/data view instead of going through the default /datawizard/[id]/auto view. However, the data view is not (currently) integrated with the Django admin flow, so you'd have to do some template customization to trigger it. I will think about some ways to make this easier to set up.

I added a new configuration option in Django Data Wizard 1.3.0 that should address this issue.