odinpastuh
The snippet can be accessed without any authentication.
Authored by
Olexandr Shalakhin
Edited
idea.py 2.08 KiB
####### models.py
from django.db import models
from django.utils.translations import ugettext_lazy as _
# adds created and updated to the model. Highly recommend to add it EVERY model
from django_extensions.db.models import TimeStampedModel
class DecimalCoinField(DecimalField):
"""
Coin field taking step of 0.1
"""
def widget_attrs(self, widget):
attrs = super().widget_attrs(widget)
attrs.update({
'step': 1,
'min': 0
})
return attrs
class CoinField(models.DecimalField):
"""
Custom field to DRY on
models.DecimalField(max_digits=64, decimal_places=8)
"""
description = _('Coins field')
def __init__(self, *args, **kwargs):
kwargs['max_digits'] = 64
kwargs['decimal_places'] = 8
super().__init__(*args, **kwargs)
def formfield(self, **kwargs):
return super().formfield(**{
'max_digits': self.max_digits,
'decimal_places': self.decimal_places,
'form_class': DecimalCoinField,
**kwargs,
})
class Algorithm(TimeStampedModel):
# only ID is enough
def __str__(self):
return f'{self.instuctions.all()}'
class Instruction(TimeStampedModel):
GTE = 1
GT = 2
LTE = 3
LT = 4
SOME_OTHER_ACTION = 5
ACTION_CHOICES = (
(GTE, _('≥')),
(GT, _('>')),
(LTE, _('≤')),
(LT, _('<')),
(SOME_OTHER_ACTION, _('Some other action')),
)
action = models.SmallPositiveIntegerField(choices=ACTION_CHOICES)
amount = CoinField(_('Amount'))
algo = models.ForeignKey(Algorithm, related_name='instructions')
class Meta:
# that is ASC ordering by `created` field
ordering = ['created']
#### admins.py
from django.contrib import admin
from .models import Algorithm
from .models import Instruction
class InstructionInline(admin.TabularInline):
model = Instruction
fields = '__all__'
@admin.register(Algorithm)
class AlgorithmAdmin(admin.ModelAdmin):
# ...
inlines = [
InstructionInline,
]
Please register or sign in to comment