Skip to content

Fix admin search fields and misc code quality issues - #12525

Open
bhumin18 wants to merge 8 commits into
inventree:masterfrom
bhumin18:fix-admin-and-code-quality-improvements
Open

Fix admin search fields and misc code quality issues#12525
bhumin18 wants to merge 8 commits into
inventree:masterfrom
bhumin18:fix-admin-and-code-quality-improvements

Conversation

@bhumin18

Copy link
Copy Markdown
Contributor

Summary

This PR fixes multiple issues across the InvenTree backend:

Admin Search Improvements

  • StockTrackingAdmin & StockItemTestResultAdmin: Add missing search_fields to enable searching stock audit logs and test results
  • PartPricingAdmin, PartStocktakeAdmin, PartRelatedAdmin, PartTestTemplateAdmin: Add missing search_fields for part-related admin models
  • SalesOrderAllocationAdmin & ReturnOrderLineItemAdmin: Add missing search_fields and improve list_display

Code Quality Fixes

  • order/serializers.py: Fix incorrect is not identity comparison (should be != value comparison) for custom status validation
  • importer/admin.py: Fix wrong super() method call (formfield_for_choice_fieldformfield_for_dbfield)
  • common/models.py: Fix broken delete() method signature on EmailMessage (*kwargs*args, **kwargs)
  • order/serializers.py, machine/registry.py, plugin/base/barcodes/mixins.py: Replace bare except: clauses with except Exception: (PEP 8 E722)

bhumin18 added 8 commits July 31, 2026 13:29
Add search_fields to StockTrackingAdmin and StockItemTestResultAdmin

- item__part__name: search tracking by part name
- item__serial: search tracking by serial number
- notes: search tracking notes
- stock_item__part__name: search test results by part name
- stock_item__serial: search test results by serial number
- template__test_name: search test results by test template name
- value: search test results by output value
- notes: search test result notes

Also adds a unit test to verify search_fields configuration.
…tTestTemplate

Add search_fields to PartPricingAdmin, PartStocktakeAdmin, PartRelatedAdmin,
and PartTestTemplateAdmin.

- part__name, part__IPN, part__description: search PartPricing
- part__name, part__IPN: search PartStocktake
- part_1__name, part_2__name: search PartRelated
- part__name, test_name, description: search PartTestTemplate

Also adds unit test assertions to verify search_fields configuration.
Add search_fields to SalesOrderAllocationAdmin and ReturnOrderLineItemAdmin.

SalesOrderAllocationAdmin:
- line__order__reference: search by Sales Order reference
- line__part__name: search by ordered Part name
- item__part__name: search by allocated Stock Item part name
- item__part__IPN: search by allocated Stock Item IPN
- item__serial: search by Stock Item serial number

ReturnOrderLineItemAdmin:
- order__reference: search by Return Order reference
- order__customer__name: search by Customer name
- item__part__name: search by returned Item part name
- item__serial: search by returned Item serial number
- reference: search by line item reference

Also adds list_display improvements and unit tests to verify
search_fields configuration.
Use '!=' (value comparison) instead of 'is not' (identity comparison)
when comparing custom_status.logical_key with self.instance.status.

Python only caches small integers (-5 to 256). For status codes > 256,
'is not' can return True even when values are equal, causing valid
custom status keys to be incorrectly rejected.

Per PEP 8: always use '==' or '!=' for value comparisons.
The formfield_for_dbfield method was incorrectly calling
super().formfield_for_choice_field() instead of super().formfield_for_dbfield().

These are different Django admin methods with different expectations.
formfield_for_choice_field expects choice-type fields, but the 'column'
field is a plain CharField. This could cause incorrect form rendering
or errors when viewing DataImportSession detail in Django Admin.

Fix: call the correct parent method formfield_for_dbfield().
The delete() method used '*kwargs' which collects positional arguments
into a tuple named 'kwargs'. This breaks Django's Model.delete()
contract which expects keyword arguments (using=None, keep_parents=False).

When super().delete(*kwargs) was called, keyword arguments passed by
Django internals would be unpacked incorrectly as positional args.

Fix: use standard '*args, **kwargs' signature and pass both to super().
Replace bare 'except:' with 'except Exception:' in
validate_status_custom_key method.

Bare except catches all BaseException subclasses including SystemExit,
KeyboardInterrupt, and MemoryError which should never be silenced.
The get_logical_value() function performs a database .get() call that
can raise ObjectDoesNotExist or MultipleObjectsReturned, both of which
are subclasses of Exception.

This follows PEP 8 (E722: do not use bare except).
Replace bare 'except:' with 'except Exception:' in two locations:

- machine/registry.py: hash computation catches AttributeError or
  DoesNotExist when a machine config no longer exists
- plugin/base/barcodes/mixins.py: has_barcode_generation property
  catches any error from calling generate(None) on a plugin

Bare except catches all BaseException subclasses including SystemExit
and KeyboardInterrupt which should never be silenced.

This follows PEP 8 (E722: do not use bare except).
@netlify

netlify Bot commented Jul 31, 2026

Copy link
Copy Markdown

Deploy Preview for inventree-web-pui-preview ready!

Name Link
🔨 Latest commit eeff12e
🔍 Latest deploy log https://app.netlify.com/projects/inventree-web-pui-preview/deploys/6a6c76f25a083700089dc795
😎 Deploy Preview https://deploy-preview-12525--inventree-web-pui-preview.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
Lighthouse
Lighthouse
1 paths audited
Performance: 97 (no change from production)
Accessibility: 81 (no change from production)
Best Practices: 100 (no change from production)
SEO: 78 (no change from production)
PWA: -
View the detailed breakdown and full score reports
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the actual benefit of this? This seems to be coverage for coverage sake.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above; why are you checking if Django admin is working correctly in 20 different asserts? This seems like bad prompting imo and puts your competence into question

raise ValidationError(_('Invalid custom status key'))

if custom_status.logical_key is not self.instance.status:
if custom_status.logical_key != self.instance.status:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Has this been verified? There is a difference between these comparison methods

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants