Hey group! I fear this is a basic python issue that I can’t seem to solve. But, posting here in Django, since it relates to a model field and attribute “verbose_name”, in hopes for faster resolution.
I am using Django-Pint. It basically uses Pint libraries and a Float Field in Models. All I did, was override it’s init() in order to format the precision of the data.
The custom field I’m extending is QuantityField:
class QuantityField(QuantityFieldMixin, models.FloatField):
form_field_class = QuantityFormField
to_number_type = float
And the parent class init() that I’m overriding:
class QuantityFieldMixin(object):
to_number_type: Callable[[Any], NUMBER_TYPE]
# TODO: Move these stuff into an Protocol or anything
# better defining a Mixin
value_from_object: Callable[[Any], Any]
name: str
validate: Callable
run_validators: Callable
"""A Django Model Field that resolves to a pint Quantity object"""
def __init__(
self,
base_units: str,
*args,
unit_choices: Optional[typing.Iterable[str]] = None,
**kwargs,
):
My code in models.py is pretty basic. I’m just taking an object inside the init and setting a format attribute. I’m creating/setting a named argument “precision”
class PrecisionQuantityField(QuantityField):
def __init__(self, base_units, *args, unit_choices, precision='.2f', **kwargs):
super(PrecisionQuantityField,self).__init__(base_units, *args, unit_choices, **kwargs)
self.ureg.default_format = precision
And the field I’m applying it to:
class RecipeItem(models.Model):
class Meta:
abstract = True #We don't want our own table. Inherit these fiels
amount_weight = PrecisionQuantityField('kilograms', null=True, blank=True, unit_choices=['lb', 'gram', 'oz', 'milligram', 'kilogram'])
amount_volume = PrecisionQuantityField('liters', null=True, blank=True, unit_choices=['floz', 'ml', 'gallon', 'liter'])
@property
def getAmount(self):
if self.amount_weight is not None:
return self.amount_weight
return self.amount_volume
class RecipeFermentable(RecipeItem):
recipe_notes = models.CharField(max_length=200, null=True, blank=True)
is_fermentable = models.BooleanField(null=True, blank=True)
fermentable = models.ForeignKey(Fermentable, on_delete=models.RESTRICT)
intended_use = models.ForeignKey(AdjunctUsage, on_delete=models.CASCADE)
recipe = models.ManyToManyField(Recipe, related_name='fermentables')
@property
def display_name(self):
if self.fermentable.supplier:
return self.fermentable.supplier + ": " + self.fermentable.name
else:
return self.fermentable.name
def __str__(self):
return self.display_name
The error I’m getting when “makemigrations” is:
TypeError: Couldn't reconstruct field amount_weight on batchthis.RecipeFermentable: __init__() got multiple values for argument 'verbose_name'
RecipeFermentable inherits from the abstract class RecipeItem, which holds the field “amount_weight” using my overriding class. It works fine without my override.
Does anyone know where I went wrong? I made sure I’m not duplicating values in my super().init() call. Or, at least, I’m pretty darn sure.
Thanks!