0

そのようなモデルで使用される平日を保存するフィールドを作成しました ( models.py):

from myfields import *
from django.db import models

class MyModel(models.Model):
    weekdays = WeekdayField()

フォーム宣言を保存したファイルはmyfields.py次のとおりです。

from django.db import models
from django import forms

class Weekdays(object):
    CHOICES = (
        (1, "Monday"),
        (2, "Tuesday"),
        (4, "Wednesday"),
        (8, "Thursday"),
        (16, "Friday"),
        (32, "Saturday"),
        (64, "Sunday"),
        )
    MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY,SUNDAY = [2**i for i in range(0,7)]
    del i

    def __init__(self, value=0):
        self.raw = value

    def __add__(self, other):
        return Weekdays(self.raw | other)

    def __sub__(self, other):
        return Weekdays(self.raw & (-1 ^ other))

    def __unicode__(self):
        ret = []
        for ele in Weekdays.CHOICES:
            if (self.raw & ele[0]) >> ele[0] == 1:
                ret.append(ele[1])
        return ",".join(ret)

    def __iter__(self):
        ret = []
        for ele in Weekdays.CHOICES:
            if (self.raw & ele[0]) >> ele[0] == 1:
                ret.append(ele)
        return iter(ret)

    def __len__(self):
        i = 0
        for n in range(7):
            if (self.raw & n) >> n == 1:
                i += 1
        return i



class WeekdayField(models.Field):
    description = "Multiple select of weekdays"

    __metaclass__ = models.SubfieldBase

    def __init__(self, *args, **kwargs):
        kwargs['max_length'] = 3
        super(WeekdayField, self).__init__(*args, **kwargs)

    def to_python(self, value):
        if isinstance(value, int):
            return [ wd for wd in Weekdays(value) ]
        if isinstance(value, Weekdays):
            return value
        if isinstance(value, list): #list of weekstay
            wd = Weekdays()
            for val in value:
                wd += int(val)
            return wd

    # PY to DB
    def get_prep_value(self, value):
        return value.raw

    def formfield(self, **kwargs):
        defaults = {'form_class': WeekdayFormField}
        defaults.update(kwargs)
        return super(WeekdayField, self).formfield(**defaults)

    def get_internal_type(self):
        return "IntegerField"


class WeekdayFormField(forms.MultipleChoiceField):
    def __init__(self, *args, **kwargs):
        if 'choices' not in kwargs:
            kwargs['choices'] = Weekdays.CHOICES
        kwargs.pop('max_length', None)
        if 'widget' not in kwargs:
            kwargs['widget'] = forms.widgets.CheckboxSelectMultiple
        super(WeekdayFormField, self).__init__(*args, **kwargs)


    def clean(self, value):
        super(WeekdayFormField, self).clean(value)
        possible_values = [ str(x[0]) for x in Weekdays.CHOICES ]
        for v in value:
            if not v in possible_values:
                raise forms.ValidationError("Day not valid")
        return value

これを使用して、Django Admin を介して要素を作成および削除できますが、値を変更すると、以前に保存した値が表示されません。データベースを調べたところ、値は正しく保存されています。

変更中に Django Admin の現在の値を確認する方法についてのアイデアはありますか?

4

1 に答える 1

0

私は解決策を持っています!Weekdays.__unicode__()メソッドとの実装が間違っていましたWeekdays.__iter__()

最後に、以下に報告されている 2 つの方法も変更しましたWeekdayField

#Class-type
class Weekdays(object):
    ...

    def __unicode__(self): # Request for the matching
        ret = []
        for i,ele in enumerate(Weekdays.CHOICES):
            if (self.raw & ele[0]) >> i == 1:
                ret.append(unicode(ele[0]))
        return ','.join(ret)

    def __iter__(self):
        for i,ele in enumerate(Weekdays.CHOICES):
            if (self.raw & ele[0]) >> i == 1:
                yield Weekdays(ele[0])


#Field to include in the model
class WeekdayField(models.Field):
    ...

    # DB to PY
    def to_python(self, value):
        if isinstance(value, int):
            return [ wd for wd in Weekdays(value) ]
        if isinstance(value, unicode):
            return [ wd for wd in Weekdays(int(value)) ]
        if isinstance(value, Weekdays):
            return value
        if isinstance(value, list): #list of Weekdays
            wd = []
            for val in value:
                wd += self.to_python(val)
            return wd

    #PY to DB
    def get_prep_value(self, value):
        if isinstance(value, Weekdays):
            return value.raw
        if isinstance(value, list):
            sum_raw = 0
            for val in value:
                sum_raw += self.get_prep_value(val)
            return sum_raw

これが誰かの役に立てば幸いです!

于 2012-09-03T18:18:13.367 に答える