Compare Django child classes of abstract model in Django - django

I have two classes as childs of abstract class (example below).
How to check if queryset model is one or other of these classes?
class Parent(models.Model)
field_a = models.CharField(primary_key = True, max_length = 24)
field_b = models.CharField(primary_key = True, max_length = 24)
class Meta:
abstract = True
class A(Parent)
pass
class B(Parent)
pass
I have tired something like this but it's not working:
if type(queryset.model) == type(A):
do something...
elif type(queryset.model) == type(B):
do something else...
because when I check the type(queryset.model) it's returns type(Parent),
even the queryset.model is the A or B class.

To be more precise issubclass is not working.
isinstance method is not working too because the object which I check is not instance.
my problem is for example when i have queryset of one of child models, and when I don't know exactly which one how do I check this?
for example:
queryset = A.objects.all()
# or
queryset = B.objects.all()
if I don't ktow the queryset (dynamic method) this check like this it returns false for boths conditions:
if type(queryset.model) == type(A):
do something...
elif type(queryset.model) == type(B):
do something else...
because true is:
type(queryset.model) == type(Parent)
my solution is to use __name__ but I don't know if there is a better way?
if queryset.model.__name__ == A.__name__:
do something...
elif queryset.model.__name__ == B.__name__:
do something else...

You should compare queryset.model directly with the class.
Something like this should work:
if queryset.model == A:
# do your stuff

Related

Python - Overriding a parent variable with a getter/setter

So I have a parent class BaseAdd that I'm trying to subclass. The BaseAdd uses self.left and self.right, I want to use self.nodes to make it easier to access both left and right at once:
class BaseAdd():
def __init__(self, leftright):
self.left = leftright[0]
self.right = leftright[1]
class Add(BaseAdd):
def __init__(self, leftright):
self.nodes = leftright
#property
def left(self):
return self.nodes[0]
#left.setter
def left(self, value):
self.nodes[0] = value
foo = Add(('L', 'R'))
foo.left = "new"
print(foo.left, foo.nodes[0])
>>> ('new', 'L')
The problem is that the setter is never getting called, my hunch is that it's using the BaseAdd.left somehow instead. How can I make the setter properly set the list element?

How to modify the form filed data?

I have a model:
class PastStudy(Model):
grade_average = FloatField(null=True)
I have a modelform as below:
class PastStudyForm(ModelForm):
class Meta:
model = PastStudy
fields = ('grade_average', )
What I have in view:
...
if request.POST:
past_study_form = PastStudyForm(request.POST)
if past_study_form.is_valid():
return HttpResponse(past_study_form.cleaned_data['grade_average'])
else:
profile_filter_past_study_form = ProfileFilterPastStudyForm()
...
What I need is that I want to write a clean method for PastStudyForm so that in case I entered 90 as grade average, HttpResponse converts it two 0-20 grading scheme and returns 18.
I tried this and I was still getting 90 not 18
class PastStudyForm(ModelForm):
class Meta:
model = PastStudy
fields = ('grade_average', )
def clean(self):
cleaned_data = super().clean()
grade_average = self.cleaned_data['grade_average']
self.cleaned_data['grade_average'] = grade_average/5
return cleaned_data
and This:
class PastStudyForm(ModelForm):
class Meta:
model = PastStudy
fields = ('grade_average', )
def clean_grade_average(self):
grade_average = self.cleaned_data['grade_average']
data = grade_average/5
return data
However, I still get 90. I also have tried a few other methods but I still was getting 90 in HttpResponse
Maybe using clean method be wrong and I should do something else!
The real code is huge and I have summarized it in here and other aspects of the problem are not explained in here. That is why I prefer and expect to get a response in which I am advised how to it in the form definition, not other methods such as converting it in the view.
in your clean method, you assign the result of your calculation method into self.cleaned_data,
while you are returning cleaned_data not self.cleaned_data.
it is different variable.
try this instead:
self.cleaned_data = super().clean()
grade_average = self.cleaned_data['grade_average']
self.cleaned_data['grade_average'] = grade_average/5
return self.cleaned_data

Call method on Django fields

class Colour(models):
...
def colour_preview(self):
return format_html(...)
class ColourTheme(models):
colour1 = models.ForeignKey(Colour)
colour2 = models.ForeignKey(Colour)
colour3 = models.ForeignKey(Colour)
...
def preview(self):
for field in self._meta.get_fields(include_parents=False):
if (field.related_model == Colour):
field.colour_preview()
I have a ColourTheme model with multiple Colour foreign keys, and I want to call a function on each of the Colour objects referred to by those fields. The last line of code above fails. I would like to call colour_preview on all Colour fields without hardcoding them (self.colour1.colour_preview() works but not ideal).
How might I achieve this?
You cannot refer to the field in order to access related object method.
Try something like this (I haven't tested it):
class ColourTheme(models):
colour1 = models.ForeignKey(Colour)
colour2 = models.ForeignKey(Colour)
colour3 = models.ForeignKey(Colour)
...
def preview(self):
for field in self._meta.get_fields(include_parents=False):
if (field.related_model == Colour):
field_obj = field.value_from_obj(self) # To get obj reference
field_obj.colour_preview()

Is there a converter between django.forms.Form and rest_framework.serializers?

I already have plenty of forms defined in my django 1.9 project. Now I need to export them as REST (DRF 3.5.3) as well.
With just a bit of hacking, I was able to provide GET and PUT methods. But I also need to provide the OPTIONS method and I can't seem to find anything that would help me do that.
So, is there something that would convert an instanced form to DRF Serializer / ViewSet?
No but you can do the other way around as explained in http://www.django-rest-framework.org/topics/html-and-forms/#rendering-forms.
drf-braces can do this.
from django import forms
from drf_braces.serializers.form_serializer import FormSerializer
class MyForm(forms.Form):
foo = forms.CharField(max_length=32)
bar = forms.DateTimeField()
class MySerializer(FormSerializer):
class Meta(object):
form = MyForm
https://github.com/dealertrack/django-rest-framework-braces/blob/master/docs/overview.rst
I didn't want to mess with all of my custom logic in the forms, so I just added an as_options method:
class APIViewForm(Form):
def as_dict(self):
return {fld.auto_id: self.initial.get(fld.name, None) for fld in self}
def as_options(self):
flds = {}
for fld in self:
flds[fld.name] = f = {}
fld = fld.field
f.update(dict(
required=fld.required,
read_only=fld.disabled,
label=fld.label,
))
if isinstance(fld, (CharField, URLField)):
f['type'] = 'field'
if fld.max_length:
f['max_length'] = fld.max_length
if fld.min_length:
f['min_length'] = fld.min_length
elif isinstance(fld, IntegerField):
f['type'] = 'integer'
if fld.max_value:
f['max_value'] = fld.max_value
if fld.min_value:
f['min_value'] = fld.min_value
elif isinstance(fld, ChoiceField):
f['type'] = 'choice'
f['choices'] = [dict(value=c[0], display_name=c[1]) for c in fld.choices]
elif isinstance(fld, DateTimeField):
f['type'] = 'datetime'
return dict(
name=self.__class__.__qualname__,
description='',
renders=['application/json'],
parses=['application/json'],
actions=dict(PUT=flds)
)
It seems to do the trick

Overload get_FIELD_display() function

Is there a way to overload Django's get_FIELD_display() function properly? When I call the function from inside itself, the result is a recursion. But I can't call it using super() either, as it's not a parent class' method but a method created by the metaclass...
The goal is to have a common interface for getting a displayable version of a CHOICE field (which is given by get_FIELD_display), but with the possibility to customize it in some specific cases.
Example:
# This does not work because it results in recursion
def get_opposition_state_display(self):
"""Overloading of default function."""
value = self.get_opposition_state_display()
if self.opposition_state == 4:
return '%s %s' % (value, self.opposition_date.strftime('%d.%m.%Y'))
return value
updated
field = self._meta.get_field('opposition_state')
value = self._get_FIELD_display(field)
To override get_FOO_display you need something like this:
field_name = models.PositiveSmallIntegerField('Field', choices=some_choices)
def _get_FIELD_display(self, field):
f_name = field.name
if f_name == 'field_name':
return 'what_you_need'
return super(YourModelName, self)._get_FIELD_display(field=field)