Django Many to Many and admin - python
I have a django ap which has a rather complicated model setup. I ended up using multi level composition to create a hierarchical model. All the relations are one to one, so I could have use inheritance but I chose not to so that i would benefit from having object composition for my models, this means I can do things like
product.outerframe.top.cost
which make the complicated calculations I have to preform, a lot better organised.
However, This model arrangement makes using the django admin tricky. I basically have a through table, i.e the outerframe table is just a bunch of foreign keys to other tables (with unique constraint on each). I ended up oerriding the add_view() and change_view() methods of ModelAdmin, which is pretty hard.
Is there an easier way to deal with many to many / through tables when using the django admin?
The tables are arranged like so:
Product > outerframe, innerframe, glass, other
outerframe > top, bottom, side etc.
innerframe > top, bottom, side etc.
glass > glass_type etc.
other > accessories etc.
Here are my models:
class Product(mixins.ProductVariables):
name = models.CharField(max_length=255)
sku = models.CharField(max_length=100, unique=True, db_index=True)
image = thumbnail.ImageField(upload_to='product_images', blank=True)
description = models.TextField(blank=True)
group = models.ForeignKey('ProductGroup', related_name='products', null=True)
hidden = models.BooleanField(default=False)
product_specific_mark_up = models.DecimalField(default=1.0, max_digits=5,decimal_places=2)
# Methods for totals
def total_material_cost(self, width, height, options):
return sum([
self.outerframe.cost(width, height, options),
self.innerframe.cost(width, height, options),
self.glass.cost(width, height, options),
self.other.cost(width, height, options),
])
def total_labour_time(self, width, height, options):
return sum([
self.outerframe.labour_time(width, height, options),
self.innerframe.labour_time(width, height, options),
self.glass.labour_time(width, height, options),
self.other.labour_time(width, height, options),
])
def total_co2_output(self, width, height, options):
return sum([
self.outerframe.co2_output(width, height, options),
self.innerframe.co2_output(width, height, options),
self.glass.co2_output(width, height, options),
self.other.co2_output(width, height, options),
])
#property
def max_overall_width(self):
return 1000
#property
def max_overall_height(self):
return 1000
def __unicode__(self):
return self.name
class OuterFrame(models.Model, mixins.GetFieldsMixin, mixins.GetRelatedClassesMixin):
top = models.OneToOneField(mixins.TopFrame)
bottom = models.OneToOneField(mixins.BottomFrame)
side = models.OneToOneField(mixins.SideFrame)
accessories = models.OneToOneField(mixins.Accessories)
flashing = models.OneToOneField(mixins.Flashing)
silicone = models.OneToOneField(mixins.Silicone)
product = models.OneToOneField(Product)
def cost(self, width, height, options):
#accessories_cost = (self.accessories.cost if options['accessories'] else 0)
#flashing_cost = (self.flashing.cost if options['flashing'] else 0)
#silicone_cost = (self.silicone.cost if options['silicone'] else 0)
return sum([
self.top.cost * (width / 1000),
self.bottom.cost * (width / 1000),
self.side.cost * (width*2 / 1000),
#accessories_cost,
#flashing_cost,
#silicone_cost,
])
def labour_time(self, width, height, options):
return datetime.timedelta(minutes=100)
def CO2_output(self, width, height, options):
return 100 # some kg measurement
#classmethod
def get_fields(cls):
options = cls._meta
fields = {}
for field in options.fields:
if field.name == 'product':
continue
if isinstance(field, models.OneToOneField):
related_cls = field.rel.to
related_fields = fields_for_model(related_cls, fields=related_cls.get_fields())
fields.update( { related_cls.__name__ + '_' + name:field for name, field in related_fields.iteritems() })
return fields
class InnerFrame(models.Model, mixins.GetFieldsMixin, mixins.GetRelatedClassesMixin):
top = models.OneToOneField(mixins.TopFrame)
bottom = models.OneToOneField(mixins.BottomFrame)
side = models.OneToOneField(mixins.SideFrame)
accessories = models.OneToOneField(mixins.Accessories)
product = models.OneToOneField(Product)
def cost(self, width, height, options):
#accessories_cost = (self.accessories.cost if options['accessories'] else 0)
print self.top.cost
return sum([
self.top.cost * (width / 1000),
self.bottom.cost * (width / 1000),
self.side.cost * (width*2 / 1000),
# accessories_cost,
])
def labour_time(self, width, height, options):
return datetime.timedelta(minutes=100)
def CO2_output(self, width, height, options):
return 100 # some kg measurement
class Glass(models.Model, mixins.GetRelatedClassesMixin):
glass_type_a = models.OneToOneField(mixins.GlassTypeA)
glass_type_b = models.OneToOneField(mixins.GlassTypeB)
enhanced = models.OneToOneField(mixins.Enhanced)
laminate = models.OneToOneField(mixins.Laminate)
low_iron = models.OneToOneField(mixins.LowIron)
privacy = models.OneToOneField(mixins.Privacy)
anti_slip = models.OneToOneField(mixins.AntiSlip)
heat_film_mirror = models.OneToOneField(mixins.HeatMirrorField)
posished_edges = models.OneToOneField(mixins.PolishedEdges)
product = models.OneToOneField(Product)
def cost(self, width, height, options):
return sum([
])
def labour_time(self, width, height, options):
return datetime.timedelta(minutes=100)
def CO2_output(self, width, height, options):
return 100 # some kg measurement
class Other(models.Model, mixins.GetRelatedClassesMixin):
num_packages = models.OneToOneField(mixins.NumberPackages)
product = models.OneToOneField(Product)
def cost(self, width, height, options):
return 100
def labour_time(self, width, height, options):
return datetime.timedelta(minutes=100)
def CO2_output(self, width, height, options):
return 100 # some kg measurement
mixins:
class TimeCostMixin(models.Model, GetFieldsMixin):
cost = models.DecimalField(default=0.0, max_digits=10, decimal_places=2)
time = models.TimeField(default=datetime.timedelta(0))
class Meta:
abstract = True
##### Frame #####
class FrameComponentMixin(TimeCostMixin):
external_width = models.IntegerField(default=0)
material_weight = models.DecimalField(default=0.0, max_digits=10,decimal_places=2)
u_value = models.DecimalField(default=0.0, max_digits=10,decimal_places=2)
class Meta:
abstract = True
class TopFrame(FrameComponentMixin):
pass
class BottomFrame(FrameComponentMixin):
pass
class SideFrame(FrameComponentMixin):
pass
class Accessories(TimeCostMixin):
material_weight = models.DecimalField(default=0.0,max_digits=10,decimal_places=2)
class Flashing(TimeCostMixin):
pass
class Silicone(TimeCostMixin):
labour_time = models.DecimalField(default=0.0, max_digits=10,decimal_places=2)
#################
##### Glass #####
class GlassTypeA(TimeCostMixin):
material_weight = models.DecimalField(default=0.0, max_digits=10,decimal_places=2)
u_value = models.DecimalField(default=0.0, max_digits=10,decimal_places=2)
class GlassTypeB(TimeCostMixin):
material_weight = models.DecimalField(default=0.0, max_digits=10,decimal_places=2)
u_value = models.DecimalField(default=0.0, max_digits=10,decimal_places=2)
class Enhanced(TimeCostMixin):
material_weight = models.DecimalField(default=0.0, max_digits=10,decimal_places=2)
class Laminate(TimeCostMixin):
material_weight = models.DecimalField(default=0.0, max_digits=10,decimal_places=2)
class LowIron(TimeCostMixin):
pass
class Privacy(TimeCostMixin):
pass
class AntiSlip(TimeCostMixin):
pass
class HeatMirrorField(TimeCostMixin):
u_value = models.DecimalField(default=0.0, max_digits=10,decimal_places=2)
class PolishedEdges(models.Model):
cost = models.DecimalField(default=0.0, max_digits=10, decimal_places=2)
##################
##### other #####
class NumberPackages(models.Model):
number_of_packages = models.IntegerField(default=0)
##################
and a hair pulling admin!
class ProductAdmin(AdminImageMixin, admin.ModelAdmin):
inlines = [ProductDownloadInline, ProductConfigurationInline]
add_form_template = 'admin/products/add_form.html'
change_form_template = 'admin/products/add_form.html'
#csrf_protect_m
#transaction.atomic
def add_view(self, request, form_url='', extra_context=None):
extra_context = extra_context or {}
"The 'add' admin view for this model."
model = self.model
opts = model._meta
if not self.has_add_permission(request):
raise PermissionDenied
ModelForm = self.get_form(request)
formsets = []
inline_instances = self.get_inline_instances(request, None)
if request.method == 'POST':
form = ModelForm(request.POST, request.FILES)
if form.is_valid():
new_object = self.save_form(request, form, change=False)
form_validated = True
else:
form_validated = False
new_object = self.model()
prefixes = {}
for FormSet, inline in zip(self.get_formsets(request), inline_instances):
prefix = FormSet.get_default_prefix()
prefixes[prefix] = prefixes.get(prefix, 0) + 1
if prefixes[prefix] != 1 or not prefix:
prefix = "%s-%s" % (prefix, prefixes[prefix])
formset = FormSet(data=request.POST, files=request.FILES,
instance=new_object,
save_as_new="_saveasnew" in request.POST,
prefix=prefix, queryset=inline.get_queryset(request))
formsets.append(formset)
#####
outer_frame_forms = [
modelform_factory(cls)(request.POST, prefix='OuterFrame_'+cls.__name__)
for cls in models.OuterFrame.get_related_classes(exclude=['product'])
]
inner_frame_forms = [
modelform_factory(cls)(request.POST, prefix='InnerFrame'+cls.__name__)
for cls in models.InnerFrame.get_related_classes(exclude=['product'])
]
glass_forms = [
modelform_factory(cls)(request.POST, prefix='InnerFrame'+cls.__name__)
for cls in models.Glass.get_related_classes(exclude=['product'])
]
other_forms = [
modelform_factory(cls)(request.POST, prefix='InnerFrame'+cls.__name__)
for cls in models.Other.get_related_classes(exclude=['product'])
]
#####
if all_valid(formsets
+outer_frame_forms
+inner_frame_forms
+glass_forms
+other_forms
) and form_validated:
self.save_model(request, new_object, form, False)
self.save_related(request, form, formsets, False)
self.log_addition(request, new_object)
##### save object hierichy #####
# inner frame
inner_frame = models.InnerFrame()
inner_frame.product = new_object
mapping = {f.rel.to:f.name for f in models.InnerFrame._meta.fields if f.name not in ['id','product']}
for f in inner_frame_forms:
obj = f.save()
setattr(inner_frame, mapping[obj.__class__], obj)
inner_frame.save()
# outer frame
outer_frame = models.OuterFrame()
outer_frame.product = new_object
mapping = {f.rel.to:f.name for f in models.OuterFrame._meta.fields if f.name not in ['id','product']}
for f in outer_frame_forms:
obj = f.save()
setattr(outer_frame, mapping[obj.__class__], obj)
outer_frame.save()
# glass
glass = models.Glass()
glass.product = new_object
mapping = {f.rel.to:f.name for f in models.Glass._meta.fields if f.name not in ['id','product']}
for f in glass_forms:
obj = f.save()
setattr(glass, mapping[obj.__class__], obj)
glass.save()
# other
other = models.Other()
other.product = new_object
mapping = {f.rel.to:f.name for f in models.Other._meta.fields if f.name not in ['id','product']}
for f in other_forms:
obj = f.save()
setattr(other, mapping[obj.__class__], obj)
other.save()
#################################
return self.response_add(request, new_object)
else:
forms = SortedDict({})
forms['Outer Frame Variables'] = {
cls.__name__: modelform_factory(cls)(prefix='OuterFrame_'+cls.__name__)
for cls in models.OuterFrame.get_related_classes(exclude=['product'])
}
forms['Inner Frame Variables'] = {
cls.__name__: modelform_factory(cls)(prefix='InnerFrame'+cls.__name__)
for cls in models.InnerFrame.get_related_classes(exclude=['product'])
}
forms['Glass Variables'] = {
cls.__name__: modelform_factory(cls)(prefix='InnerFrame'+cls.__name__)
for cls in models.Glass.get_related_classes(exclude=['product'])
}
forms['Other Variables'] = {
cls.__name__: modelform_factory(cls)(prefix='InnerFrame'+cls.__name__)
for cls in models.Other.get_related_classes(exclude=['product'])
}
extra_context['forms'] = forms
# Prepare the dict of initial data from the request.
# We have to special-case M2Ms as a list of comma-separated PKs.
initial = dict(request.GET.items())
for k in initial:
try:
f = opts.get_field(k)
except models.FieldDoesNotExist:
continue
if isinstance(f, models.ManyToManyField):
initial[k] = initial[k].split(",")
form = ModelForm(initial=initial)
prefixes = {}
for FormSet, inline in zip(self.get_formsets(request), inline_instances):
prefix = FormSet.get_default_prefix()
prefixes[prefix] = prefixes.get(prefix, 0) + 1
if prefixes[prefix] != 1 or not prefix:
prefix = "%s-%s" % (prefix, prefixes[prefix])
formset = FormSet(instance=self.model(), prefix=prefix,
queryset=inline.get_queryset(request))
formsets.append(formset)
adminForm = helpers.AdminForm(form, list(self.get_fieldsets(request)),
self.get_prepopulated_fields(request),
self.get_readonly_fields(request),
model_admin=self)
media = self.media + adminForm.media
inline_admin_formsets = []
for inline, formset in zip(inline_instances, formsets):
fieldsets = list(inline.get_fieldsets(request))
readonly = list(inline.get_readonly_fields(request))
prepopulated = dict(inline.get_prepopulated_fields(request))
inline_admin_formset = helpers.InlineAdminFormSet(inline, formset,
fieldsets, prepopulated, readonly, model_admin=self)
inline_admin_formsets.append(inline_admin_formset)
media = media + inline_admin_formset.media
context = {
'title': _('Add %s') % force_text(opts.verbose_name),
'adminform': adminForm,
'is_popup': IS_POPUP_VAR in request.REQUEST,
'media': media,
'inline_admin_formsets': inline_admin_formsets,
'errors': helpers.AdminErrorList(form, formsets),
'app_label': opts.app_label,
'preserved_filters': self.get_preserved_filters(request),
}
context.update(extra_context or {})
return self.render_change_form(request, context, form_url=form_url, add=True)
I haven't fully processed your lengthy add_view method, but the answer to your general question is simply "No." The admin doesn't provide any good way to handle multi-layer heterogeneous hierarchies. Two-layer hierarchies are handled nicely by inlines, and so you can easily make it so that from editing an object in any one layer, you can conveniently manage related objects in the next layer down; but nothing beyond that.
There has been a ticket open for years to add nested-inline support to the admin, which would help to handle this situation. But there are lots of tricky edge-cases and it's very hard to make the UI understandable, so the patch has never reached a commit-ready state.
At some point the complexity of your data model is just beyond what the generic admin interface can handle with good usability, and you're better off just writing your own customized admin interface. Mostly the admin is just built on top of ModelForms and InlineModelFormsets, so it's not as hard as you might think to just build your own that works the way you want; it's often easier (and with better results) than trying to heavily customize the admin.
I should also mention that it is possible to use admin inlines for many-to-many through tables (even if the through table is implicit, not its own model class), as it's not immediately obvious how to access the implicitly-created through model:
class MyM2MInline(admin.TabularInline):
model = SomeModel.m2m_field.through
Related
Python Class Function not defined
I'm creating a class with function within, but i keep getting error "name 'direct_report' is not defined" Basically im tring to make an organization chart, creating a list using the direct_report function to add people under each position class employee: def __init__(self, name , title, salary): self.name = name self.title = title self.salary = salary self.direct_reports_list = direct_report() def __str__(self): #otheremp_list = [] print(self.title,'-', self.name) print('Direct Reports:') for emp in self.direct_reports_list: print(emp.title,'-', emp.name) # otheremp_list.append(emp.direct_reports_list) #print('other employees:') #for emp in otheremp_list: # print(emp.title,'-', emp.name) # otheremp_list.append(emp.direct_reports_list) def direct_report(self,value): print(value) direct_reports_list = [] direct_reports_list.append(value) print(direct_reports_list) return direct_reports_list ceo = employee("Elon Musk", "CEO",1000000) devdir = employee("Jeff Bezos","Development Director",500000) devassoc1 = employee("Beyonce Knowles","Development Associate", 50000) devassoc2 = employee("Taylor Swift","Development Associate", 50000) ceo.direct_report(devdir) ceo.direct_report(devdir2) devdir.direct_report(devassoc1) devdir.direct_report(devassoc2) print(ceo) The # is my further plan to print the full organization chart, but currently im still stuck at the "direct report" parts
You need to add one indentation level for the classes methods like that: class employee: def __init__(self, name , title, salary): self.name = name self.title = title self.salary = salary self.direct_reports_list = direct_report() def __str__(self): #otheremp_list = [] print(self.title,'-', self.name) print('Direct Reports:') for emp in self.direct_reports_list: print(emp.title,'-', emp.name) # otheremp_list.append(emp.direct_reports_list) #print('other employees:') #for emp in otheremp_list: # print(emp.title,'-', emp.name) # otheremp_list.append(emp.direct_reports_list) def direct_report(self,value): print(value) direct_reports_list = [] direct_reports_list.append(value) print(direct_reports_list) return direct_reports_list ceo = employee("Elon Musk", "CEO",1000000) devdir = employee("Jeff Bezos","Development Director",500000) devassoc1 = employee("Beyonce Knowles","Development Associate", 50000) devassoc2 = employee("Taylor Swift","Development Associate", 50000) ceo.direct_report(devdir) ceo.direct_report(devdir2) devdir.direct_report(devassoc1) devdir.direct_report(devassoc2) print(ceo)
Call the function in this way. class employee: def __init__(self, name , title, salary): self.name = name self.title = title self.salary = salary self.direct_reports_list = self.direct_report()
try self.direct_report() instead as you are calling a method within the class
Try this code. class employee: def __init__(self, name , title, salary): self.name = name self.title = title self.salary = salary self.direct_reports_list = [] def __str__(self): #otheremp_list = [] print(self.title,'-', self.name) print('Direct Reports:') for emp in self.direct_reports_list: print(emp.title,'-', emp.name) # otheremp_list.append(emp.direct_reports_list) #print('other employees:') #for emp in otheremp_list: # print(emp.title,'-', emp.name) # otheremp_list.append(emp.direct_reports_list) return "Done" def direct_report(self,value): self.direct_reports_list.append(value) ceo = employee("Elon Musk", "CEO",1000000) devdir = employee("Jeff Bezos","Development Director",500000) devassoc1 = employee("Beyonce Knowles","Development Associate", 50000) devassoc2 = employee("Taylor Swift","Development Associate", 50000) ceo.direct_report(devdir) devdir.direct_report(devassoc1) devdir.direct_report(devassoc2) print(ceo) print(devdir) print(devassoc1) print(devassoc2)
Python Composite Class JSON Serialization
I have what I would term a composite class Whereby the class LegoSet is constructed from an instantiation of the class LegoSetVariant and inturn the class LegoSetVariant is constructed from a number of different class instantiations. This is fine and I can successfully initialize the class. However, I then want to serialize the class object into JSON. This is proving a bit tricky as the composite classes are erroring as non-serializable. class LegoSet: def __init__( self, typename, id, productCode, name, slug, primaryImage, baseImgUrl, overrideUrl, variant, ): self.__typename = typename self.__id = id self.__productCode = productCode self.__name = name self.__slug = slug self.__primaryImage = primaryImage self.__baseImgUrl = baseImgUrl self.__overrideUrl = overrideUrl self.__variant = variant class LegoSetVariant: def __init__(self, id, sku, salePercentage, attributes, price, listPrice): self.__id = id self.__sku = sku self.__salePercentage = salePercentage self.__atributes = attributes self.__price = price self.__listPrice = listPrice class LegoSetVariantAttributes: def __init__( self, rating, maxOrderQuantity, availabilityStatus, availabilityText, vipAvailabilityStatus, vipAvailabilityText, canAddToBag, canAddToWishlist, vipCanAddToBag, onSale, isNew, featuredFlags, ): self.__rating = rating self.__maxOrderQuantity = maxOrderQuantity self.__availabilityStatus = availabilityStatus self.__availabilityText = availabilityText self.__vipAvailabilityStatus = vipAvailabilityStatus self.__vipAvailabilityText = vipAvailabilityText self.__canAddToBag = canAddToBag self.__canAddToWishlist = canAddToWishlist self.__vipCanAddToBag = vipCanAddToBag self.__onSale = onSale self.__isNew = isNew self.__featuredFlags = featuredFlags class LegoSetVariantAttributesFeaturedFlags: def __init__(self, key, label): self.__key = key self.__label = label class LegoSetVariantPrice: def __init__(self, formattedAmount, centAmount, currencyCode, formattedValue): self.__formattedAmount = formattedAmount self.__centAmount = centAmount self.__currencyCode = currencyCode self.__formattedValue = formattedValue class LegoSetVariantListPrice: def __init__(self, formattedAmount, centAmount): self.__formattedAmount = formattedAmount self.__centAmount = centAmount
Try this: def unpack_into_dict(object): res = {} for key, val in object.__dict__.items(): if hasattr(val, '__dict__'): res[key] = unpack_into_dict(val) else: res[key] = val return res
How to filter Django object to get top X number of objects with highest property value
So I have a class called Hero with 150 objects. Each object has a property Winrate. I want to get the top 12 heros based on winrate. class Hero(models.Model): hero_name = models.CharField(max_length=20, default = 'Dota 2 Hero') hero_id = models.IntegerField() def __str__(self): return str(self.hero_id) def get_winrate(self): wins = len(Match.objects.filter(heros_won = Hero.objects.get(hero_id = self.hero_id))) losses = len(Match.objects.filter(heros_lost = Hero.objects.get(hero_id = self.hero_id))) if wins + losses != 0: return round((wins / (wins + losses)),2) else: return 0 winrate = property(get_winrate) I tried alot of filters but couldn't get it to work.
I would make winrate an attribute of your Hero class as following. class Hero(models.Model): hero_name = models.CharField(max_length=20, default = 'Dota 2 Hero') hero_id = models.IntegerField() winrate = models.IntegerField() def _get_winrate(self): wins = len(Match.objects.filter(heros_won = Hero.objects.get(hero_id = self.hero_id))) losses = len(Match.objects.filter(heros_lost = Hero.objects.get(hero_id = self.hero_id))) if wins + losses != 0: return round((wins / (wins + losses)),2) else: return 0 def save(*args, **kwargs): self.winrate = self._getwinrate() return super().save(*args, **kwargs) Then you'll be able to order your request. super_heroes = Hero.objects.order_by('-winrate')[:12] EDIT: you shouldn't use len() on a queryset but count() like this: wins = Match.objects.filter(heros_won=self.pk).count() Why don't you use the natural primary key instead of this hero_id?
Django: cannot get an Model instance from ForwardManyToOneDescriptor (ForeignKey)
I have the following code in accounts/signals/__init__.py: from django.db.models.signals import post_save from django.dispatch import receiver from orders.models import Order from accounts.models import Balance #receiver(post_save, sender=Order) def update_referral_balance(sender, **kwargs): if len(sender.user.referrals_set.all()): # TODO: Add referralTransaction new_referral_revenue = sender.user.referrals_set.get().revenue revenue_from_trade = \ new_referral_revenue - sender.old_referral_revenue balance, created = \ Balance.objects.get(user=sender.user, currency=sender.currency) balance.balance += revenue_from_trade balance.save() Now, when running tests I am getting the following error:====================================================================== ERROR: test_orders_with_approved_payments (payments.tests.test_views.PaymentReleaseTestCase) ---------------------------------------------------------------------- Traceback (most recent call last): File "/pipeline/source/payments/tests/test_views.py", line 75, in setUp self.order.save() File "/pipeline/source/orders/models.py", line 63, in save super(Order, self).save(*args, **kwargs) File "/usr/local/lib/python3.5/site-packages/safedelete/models.py", line 64, in save super(Model, self).save(**kwargs) File "/usr/local/lib/python3.5/site-packages/django/db/models/base.py", line 708, in save force_update=force_update, update_fields=update_fields) File "/usr/local/lib/python3.5/site-packages/django/db/models/base.py", line 745, in save_base update_fields=update_fields, raw=raw, using=using) File "/usr/local/lib/python3.5/site-packages/django/dispatch/dispatcher.py", line 192, in send response = receiver(signal=self, sender=sender, **named) File "/pipeline/source/accounts/signals/__init__.py", line 9, in update_referral_balance if len(sender.user.referral_set.all()): AttributeError: 'ForwardManyToOneDescriptor' object has no attribute 'referral_set' And indeed, when running through it in debugger, I see that the sender.user attribute is something of instance ForwardManyToOneDescriptor: ipdb> pprint(sender.__dict__['user'].__dict__) {'cache_name': '_user_cache', 'field': <django.db.models.fields.related.ForeignKey: user>} What am I doing wrong? EDIT: My Order Model: class Order(TimeStampedModel, SoftDeletableModel, UniqueFieldMixin): USD = "USD" RUB = "RUB" EUR = "EUR" BUY = 1 SELL = 0 TYPES = ( (SELL, 'SELL'), (BUY, 'BUY'), ) # Todo: inherit from BTC base?, move lengths to settings? order_type = models.IntegerField(choices=TYPES, default=BUY) amount_cash = models.DecimalField(max_digits=12, decimal_places=2) amount_btc = models.DecimalField(max_digits=18, decimal_places=8) currency = models.ForeignKey(Currency) payment_window = models.IntegerField(default=settings.PAYMENT_WINDOW) user = models.ForeignKey(User, related_name='orders') is_paid = models.BooleanField(default=False) is_released = models.BooleanField(default=False) is_completed = models.BooleanField(default=False) is_failed = models.BooleanField(default=False) unique_reference = models.CharField( max_length=settings.UNIQUE_REFERENCE_LENGTH, unique=True) admin_comment = models.CharField(max_length=200) payment_preference = models.ForeignKey('payments.PaymentPreference', default=None, null=True) class Meta: ordering = ['-created_on'] def save(self, *args, **kwargs): self.unique_reference = \ self.gen_unique_value( lambda x: get_random_string(x), lambda x: Order.objects.filter(unique_reference=x).count(), settings.UNIQUE_REFERENCE_LENGTH ) self.convert_coin_to_cash() if 'is_completed' in kwargs and\ kwargs['is_completed'] and\ not self.is_completed: self.old_referral_revenue = \ self.user.referral_set.get().revenue super(Order, self).save(*args, **kwargs) def convert_coin_to_cash(self): self.amount_btc = Decimal(self.amount_btc) queryset = Price.objects.filter().order_by('-id')[:2] price_sell = [price for price in queryset if price.type == Price.SELL] price_buy = [price for price in queryset if price.type == Price.BUY] # Below calculation affect real money the client pays assert all([len(price_sell), price_sell[0].price_usd, price_buy[0].price_rub, price_buy[0].price_eur]) assert all([len(price_buy), price_buy[0].price_usd, price_buy[0].price_rub, price_buy[0].price_eur]) # TODO: Make this logic more generic, # TODO: migrate to using currency through payment_preference # SELL self.amount_cash = Decimal(self.amount_btc) if self.order_type == Order.SELL and self.currency.code == Order.USD: self.amount_cash *= price_buy[0].price_usd elif self.order_type == Order.SELL and self.currency.code == Order.RUB: self.amount_cash *= price_buy[0].price_rub elif self.order_type == Order.SELL and self.currency.code == Order.EUR: self.amount_cash *= price_buy[0].price_eur # BUY if self.order_type == Order.BUY and self.currency.code == Order.USD: self.amount_cash *= price_sell[0].price_usd elif self.order_type == Order.BUY and self.currency.code == Order.RUB: self.amount_cash *= price_sell[0].price_rub elif self.order_type == Order.BUY and self.currency.code == Order.EUR: self.amount_cash *= price_sell[0].price_eur self.amount_cash = money_format(self.amount_cash) #property def is_buy(self): return self.order_type #property def payment_deadline(self): """returns datetime of payment_deadline (creation + payment_window)""" # TODO: Use this for pay until message on 'order success' screen return self.created_on + timedelta(minutes=self.payment_window) #property def expired(self): """Is expired if payment_deadline is exceeded and it's not paid yet""" # TODO: validate this business rule # TODO: Refactor, it is unreasonable to have different standards of # time in the DB return (timezone.now() > self.payment_deadline) and\ (not self.is_paid) and not self.is_released #property def payment_status_frozen(self): """return a boolean indicating if order can be updated Order is frozen if it is expired or has been paid """ # TODO: validate this business rule return self.expired or \ (self.is_paid and self.payment_set.last() and self.payment_set.last(). payment_preference. payment_method.is_internal) #property def withdrawal_address_frozen(self): """return bool whether the withdraw address can be changed""" return self.is_released #property def has_withdraw_address(self): """return a boolean indicating if order has a withdraw adrress defined """ # TODO: Validate this business rule return len(self.address_set.all()) > 0 #property def withdraw_address(self): addr = None if self.has_withdraw_address: addr = self.transaction_set.first().address_to.address return addr def __str__(self): return "{} {} {} BTC {} {}".format(self.user.username or self.user.profile.phone, self.order_type, self.amount_btc, self.amount_cash, self.currency)
The sender argument is the model class the signal has connected to. As you can see from the signals docs, in post_save the instance is passed in a separate argument unsurprisingly called instance. You should write your handler like this: #receiver(post_save, sender=Order) def update_referral_balance(sender, instance, **kwargs): if len(instance.user.referrals_set.all()): etc, changing sender to instance throughout.
How to render DateField with 3 selects
I'm looking for the simplest and cleanest way to render a basic DateField with 3 select. <select>day</select><select>month</select><select>year</select> (and if possible use "format" to choose how to display the final render)
You can take advantage of the fact that DateField will handle multiple-valued inputs and join them together with a space, so you can avoid a secondary form and instead just provide a sequence of inputs: from wtforms.widgets.core import Select, HTMLString, html_params class SelectDateWidget(object): FORMAT_CHOICES = { '%d': [(x, str(x)) for x in range(1, 32)], '%m': [(x, str(x)) for x in range(1, 13)], '%y': [(x, str(x)) for x in range(1950, 2014)], } def __call__(self, field, **kwargs): field_id = kwargs.pop('id', field.id) html = [] for format in field.format.split(): choices = self.FORMAT_CHOICES[format] id_suffix = format.replace('%', '-') params = dict(kwargs, name=field.name, id=field_id + id_suffix) html.append('<select %s>' % html_params(params)) if field.data: current_value = int(field.data.strftime(format)) else: current_value = None for value, label in choices: selected = (value == current_value) html.append(Select.render_option(value, label, selected)) html.append('</select>') return HTMLString(''.join(html)) # Usage class MyForm(Form): american_date = DateField(format='%m %d %y', widget=SelectDateWidget()) european_date = DateField(format='%d %m %y', widget=SelectDateWidget())
Final widget: (Support multiple format not only spaces) class SelectDateWidget(object): FORMAT_CHOICES = { '%d': [(x, str(x)) for x in range(1, 32)], '%m': [(x, str(x)) for x in range(1, 13)] } FORMAT_CLASSES = { '%d': 'select_date_day', '%m': 'select_date_month', '%Y': 'select_date_year' } def __init__(self, years=range(1930, 2014)): super(SelectDateWidget, self).__init__() self.FORMAT_CHOICES['%Y'] = [(x, str(x)) for x in years] def __call__(self, field, **kwargs): field_id = kwargs.pop('id', field.id) html = [] allowed_format = ['%d', '%m', '%Y'] for format in field.format.split(): if (format in allowed_format): choices = self.FORMAT_CHOICES[format] id_suffix = format.replace('%', '-') id_current = field_id + id_suffix kwargs['class'] = self.FORMAT_CLASSES[format] try: del kwargs['placeholder'] except: pass html.append('<select %s>' % html_params(name=field.name, id=id_current, **kwargs)) if field.data: current_value = int(field.data.strftime(format)) else: current_value = None for value, label in choices: selected = (value == current_value) html.append(Select.render_option(value, label, selected)) html.append('</select>') else: html.append(format) html.append('<input type="hidden" value="'+format+'" %s></input>' % html_params(name=field.name, id=id_current, **kwargs)) html.append(' ') return HTMLString(''.join(html))
I used a custom widget and it's working but it's not perfect yet. Other ideas to do this? class SelectDateWidget(object): class SelectDateForm(Form): days = [(x,x) for x in range(1,32)] months = [(x,x) for x in range(1,13)] years = [(x,x) for x in range(1950,2014)] days_select = SelectField(choices=days) month_select = SelectField(choices=months) year_select = SelectField(choices=years) def __call__(self, field, **kwargs): kwargs.setdefault('id', field.id) date_form = self.SelectDateForm(prefix=field.name) days_html = date_form.days_select(class_="input-mini").__html__() month_html = date_form.month_select(class_="input-mini").__html__() year_html = date_form.year_select(class_="input-small").__html__() widget_html = field.format widget_html = widget_html.replace('%d', days_html) widget_html = widget_html.replace('%m', month_html) widget_html = widget_html.replace('%Y', year_html) return HTMLString(widget_html) class SelectDateField(DateField): widget = SelectDateWidget() def __init__(self, label=None, validators=None, **kwargs): super(SelectDateField, self).__init__(label, validators, **kwargs) def pre_validate(self, extra): form = SelectDateWidget.SelectDateForm(prefix=self.name) try: date = datetime.datetime.strptime(form.days_select.data+'-'+form.month_select.data+'-'+form.year_select.data, '%d-%m-%Y') self.data = date except: raise StopValidation(gettext(u'Invalid date.')) class MyForm(Form): date = SelectDateField(u'Date', validators=[Required(message=_(u'This field is required.'))], format='%d / %m / %Y')