Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
stanbird08 opened this issue Nov 7, 2019 · 3 comments
Closed
Labels
Milestone

Comments

@stanbird08
Copy link

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'),)
@sheppard
Copy link
Member

sheppard commented Feb 4, 2020

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?

@sheppard
Copy link
Member

sheppard commented Feb 4, 2020

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.

@sheppard sheppard added this to the 1.3 milestone Feb 5, 2020
@sheppard
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants