django-model-utils 의 사용 예
아래는 django-model-utils 를 이용해서 status 를 설정하고, serializer 부분은 django rest-framework 의 ModelSerializer 를 이용했다.from django.db import models # Create your models here. from model_utils import Choices from model_utils.models import StatusModel, TimeStampedModel class RmtManager(models.Manager): """ Model Manager class """ use_for_related_fields = True class Remittance(TimeStampedModel, StatusModel): """ """ STATUS = Choices('borned_remit', 'borned_payin', 'noticed_remit', 'noticed_payin', 'done_remit', 'done_payin') # If you change the variable name, # you should change the value on serializers.py # i.e. RemittanceSerializer from_user = models.ForeignKey(User) short_url = models.ForeignKey(ShortUrl, null=True) to_name = models.CharField(max_length=100) to_pnumber = models.CharField(max_length=50) amount = models.IntegerField() # int is ok for less than 2.1 billion remit_token = models.CharField(max_length=128, blank=True) hit_count = models.IntegerField(default=0) # for custom permission # See : # https://docs.djangoproject.com/en/1.8/topics/auth/customizing/#custom-permissions # add our custom model manager objects = RmtManager()
from rest_framework import serializers from .models import Remittance class RemittanceSerializer(serializers.ModelSerializer): class Meta: model = Remittance fields = ('created', 'modified', 'status', 'from_user', 'to_name', 'to_pnumber', 'amount', 'remit_token', 'short_url', 'hit_count')
Status field 에 assign
remit = Remittance() remit.status = Remittance.STATUS.borned_remit remit.save()
StatusModel 은 StatusField 와 MonitorField 를 기본적으로 가지고 있다. 그러므로 굳이 상속받지 않아도, 위 2개의 field 를 가지고 같은 효과를 낼 수 있다.
댓글 없음:
댓글 쓰기