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
Stop monkey patching models in pydata/apps.py
#1075
Comments
Django Oscar and many other Django frameworks employ a similar tactic to deal with customizations. See django-oscar docs. We can follow it and make AMY modular in the long run. |
I've realized that converting The way Django Oscar customizes models is (for me) quite tricky and I don't really understand it. I propose to create class Sponsorship(models.Model):
...
if settings.ENABLE_PYDATA:
LEVELS = (
(0, 'Founding'),
(15000, 'Diamond'),
(8000, 'Platinum'),
(5000, 'Gold'),
(3000, 'Silver'),
(1500, 'Supporting'),
(1, 'Community'),
)
amount = models.DecimalField(
max_digits=8, decimal_places=2,
blank=True, null=True,
validators=[MinValueValidator(0)],
verbose_name='Sponsorship amount',
help_text='e.g. 1992.33',
choices=LEVELS,
)
else:
amount = models.DecimalField(
max_digits=8, decimal_places=2,
blank=True, null=True,
validators=[MinValueValidator(0)],
verbose_name='Sponsorship amount',
help_text='e.g. 1992.33'
)
class Meta:
unique_together = ('organization', 'event', 'amount')
if settings.ENABLE_PYDATA:
def __str__(self):
return '{}: {}'.format(self.organization, self.amount)
else:
def __str__(self):
return '{}: {}'.format(self.organization, self.get_amount_display()) Example of forms: class SponsorshipForm(forms.ModelForm):
...
if settings.ENABLE_PYDATA:
class Media:
js = ('import_sponsor.js', )
amount = forms.ChoiceField(choices=Sponsorship.LEVELS) This is not the most beautiful solution, but IMO better than the current one. The biggest drawback is that you need to be very careful when generating migrations to make sure they work for both standard and pydata deployment. @narayanaditya95 @pbanaszkiewicz any opinions? |
-1. @pbanaszkiewicz and I wanted a complete separation of code. My suggestion would be to inherit the from workshops.models import Sponsorship as SponsorshipBase
class Sponsorship(SponsorshipBase):
LEVELS = (
(0, 'Founding'),
(15000, 'Diamond'),
(8000, 'Platinum'),
(5000, 'Gold'),
(3000, 'Silver'),
(1500, 'Supporting'),
(1, 'Community'),
)
amount = models.DecimalField(
max_digits=8, decimal_places=2,
blank=True, null=True,
validators=[MinValueValidator(0)],
verbose_name='Sponsorship amount',
help_text='e.g. 1992.33',
choices=LEVELS,
) We would then define its corresponding ModelForm and assign it to from pydata.models import Sponsorship
class SponsorshipForm(forms.ModelForm):
class Meta:
model = Sponsorship
class Media:
js = ('import_sponsor.js') In from django.conf import settings
SponsorshipForm = settings.SPONSORSHIP_FORM |
And
I'm a bit lost now. @pbanaszkiewicz @narayanaditya95 could you write a short summary what is your preferred solution to this issue and standard/pydata deployment? In my case it's:
|
We would then have to divide |
OK, so in the case of pydata deployment, we have two non-abstract models I think this solution is much better than monkey-patching and I'm 👍 (even if it results in an unnecessary empty table). Your solution to forms monkey-patching (that is, |
Yes. We can avoid having a redundant table by creating a
Yes. |
OK, this is something I can live with. It seems that we reached consensus. :-) @pbanaszkiewicz any opinions on the proposed design? |
@pbanaszkiewicz Since this affected the work with PyData, do we need to keep this open? |
@maneesha no, we can close this. I'd like to move PyData out into separate project here on GH and move all corresponding issues there. |
I've raised this issue in #1073:
An enhancement for this issue is to get rid of monkey patching of templates, forms and views, but it's not that much important right now.
The text was updated successfully, but these errors were encountered: