-
-
Notifications
You must be signed in to change notification settings - Fork 782
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
Issue with adding rows with generated field #1779
Comments
Thanks for raising. I couldn't reproduce with the example app. I created a test branch here. I added this code to the Book model: I ran the new migrations and then tried to import with the attached file. The import worked fine for me. Is it possible you can reproduce using the branch and example file? Or perhaps you can give me clearer steps to reproduce? |
Slightly adjusted tables below class OperationsExportShipments(models.Model):
shipment_id = models.CharField(primary_key=True)
shipment_status = models.ForeignKey(OperationsExportShipmentsStatuses, models.DO_NOTHING, db_column='shipment_status')
checked_in = models.BooleanField()
start_date = models.DateField(blank=True, null=True)
end_date = models.DateField(blank=True, null=True)
fba_shipment = models.OneToOneField(FbaShipments, models.DO_NOTHING, db_column='fba_shipment',null=True,blank=True)
walmart_shipment = models.OneToOneField(WalmartShipments, models.DO_NOTHING, db_column='walmart_shipment',null=True,blank=True)
shipment_carrier = models.ForeignKey(WarehouseCarriers, models.DO_NOTHING, db_column='shipment_carrier',null=True,blank=True)
mock_generated_column = models.GeneratedField(
expression=RawSQL("1", []),
output_field=models.IntegerField(),
db_persist=True
)
class Meta:
managed = True
db_table = 'operations_export_shipments'
#contraint that exactly one of fba_shipment or walmart_shipment must be set
constraints = [
models.CheckConstraint(
check=(
(Q(fba_shipment__isnull=False) & Q(walmart_shipment__isnull=True)) |
(Q(fba_shipment__isnull=True) & Q(walmart_shipment__isnull=False))
),
name='check_fba_walmart_shipment_exclusivity'
),
#constraint that checked_in can only be True if shipment_status is 'ARRIVED'
models.CheckConstraint(
check=(
Q(checked_in=False) |
(Q(checked_in=True) & Q(shipment_status='ARRIVED'))
),
name='check_checked_in_with_status_arrived'
)
]
class OperationsExportShipmentsResource(resources.ModelResource):
class Meta:
model = OperationsExportShipments
fields = (
"fba_shipment","walmart_shipment",
"shipment_status",
"checked_in"
)
def before_import_row(self, row, **kwargs):
"""
Override this method to add logic before saving an instance.
Use this to determine if a row corresponds to an FBA shipment or Walmart shipment and handle accordingly.
"""
row["shipment_status"] = row["shipment_status"].upper().replace(" ","_")
if not row["fba_shipment"]:
row["fba_shipment"] = None
if not row["walmart_shipment"]:
row["walmart_shipment"] = None
if row["checked_in"].lower().strip() == "yes":
row["checked_in"] = True
elif row["checked_in"].lower().strip() == "no":
row["checked_in"] = False
else:
row["checked_in"] = None
print(row)
super().before_import_row(row, **kwargs)
def get_instance(self, instance_loader, row):
"""
Override to custom handle identifying existing instances.
"""
# print(row)
# self.Meta.model.objects.get(fba_shipment=row['fba_shipment'],
# walmart_shipment=row['walmart_shipment'])
print("stage 2")
try:
return self.Meta.model.objects.get(fba_shipment=row['fba_shipment'],
walmart_shipment=row['walmart_shipment'])
except self.Meta.model.DoesNotExist:
print("no instance foudn!")
return None When I upload a csv with a new row on this, I get the following error: Line number: 6 - Save with update_fields did not affect any rows.
So the updated rows were fine with the import, but there was an issue with the new row. Now I comment out the generated column
And the upload is fine |
Thanks - I can reproduce when uploading a new row. |
Have raised as Django issue |
Confirmed this is an issue in Django 5.0 rather than |
Thanks @sarahboyce for the update and fix. |
Describe the bug
Import of rows via django admin breaks when I have a generated column, but works with that field commented out.
To Reproduce
Steps to reproduce the behavior:
Import of rows for a model with a generated field.
Versions (please complete the following information):
Expected behavior
Import of rows for a table.
The import works fine when I have no generated column, but breaks when the table has a generated column
Import of my spreadsheet works fine with the cost generated field commented out, but breaks when it is present
Screenshots
I can DM these but as it's company code and data I cannot post here.
Additional context
Add any other context about the problem here.
The text was updated successfully, but these errors were encountered: