V
V
vagitaku2019-05-23 16:41:40
Django
vagitaku, 2019-05-23 16:41:40

Is it possible to pull out data from models.p (from the __str__ function) in admin.py?

In mdoles.py I created the days variable (in def str). You need to somehow insert it into list_display in admin.py.
models.py:

class DriverWorkDay(models.Model):
    driver = models.OneToOneField(
        AdvancedUser,
        on_delete=models.CASCADE,
        verbose_name='Водитель')
    monday = models.BooleanField(
        default=False,
        verbose_name='Понедельник')
    tuesday = models.BooleanField(
        default=False,
        verbose_name='Вторник')
    wednesday = models.BooleanField(
        default=False,
        verbose_name='Среда')
    thursday = models.BooleanField(
        default=False,
        verbose_name='Четверг')
    friday = models.BooleanField(
        default=False,
        verbose_name='Пятница')
    saturday = models.BooleanField(
        default=False,
        verbose_name='Суббота')
    sunday = models.BooleanField(
        default=False, 
        verbose_name='Воскресенье')
    time_from = models.TimeField(
        null=False,default= '09:00:00',
        verbose_name='Время с')
    time_to = models.TimeField(
        null=False,default= '19:00:00',
        verbose_name='Время до')

    def __str__(self):
        days = []
        if self.monday:
            days.append('понидельник')
        if self.tuesday:
            days.append('вторник')
        if self.wednesday:
            days.append('среда')
        if self.thursday:
            days.append('чтверг')
        if self.friday:
            days.append('пятница')
        if self.saturday:
            days.append('суббота')
        if self.sunday:
            days.append('воскресенье')
        days = ', '.join([str(x) for x in days])
        time = f'(С {self.time_from} по {self.time_to})'
        return f'({self.driver.user.last_name} {self.driver.user.first_name} -- {days}, {time})'

    class Meta:
        verbose_name = 'Рабочий день водителя'
        verbose_name_plural = 'Рабочии дни водителей'

admin.py:
class DriverWorkDayAdmin(admin.ModelAdmin):
    list_display = ['driver', 'get_work_days', 'get_times']
    search_fields = ['driver']

    def get_work_days(self, obj):
        return obj.days

    def get_times(self, obj):
        return None

    class Meta:
        model = DriverWorkDay

    def get_form(self, request, obj=None, **kwargs):
        form = super().get_form(request, obj, **kwargs)
        driver = form.base_fields["driver"]

        driver.widget.can_add_related = False
        driver.widget.can_change_related = False

        return form

Gives this error -
'DriverWorkDay' object has no attribute 'days'

Or is it possible only in admins.py to create a string based on working days?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
FulTupFul, 2019-05-23
@vagitaku

Well, you yourself use the ModelAdmin methods for display. Just return str(obj) in the get_work_days method
This is bad practice though. It would be more correct to write a separate method that will return you the days of the week.
In models.py:

class DriverWorkDay(models.Model):
    ..................
    @property
    def days(self):
        days = []
        if self.monday:
            days.append('понидельник')
        if self.tuesday:
            days.append('вторник')
        if self.wednesday:
            days.append('среда')
        if self.thursday:
            days.append('чтверг')
        if self.friday:
            days.append('пятница')
        if self.saturday:
            days.append('суббота')
        if self.sunday:
            days.append('воскресенье')
        days = ', '.join([str(x) for x in days])
        time = f'(С {self.time_from} по {self.time_to})'
        return f'({self.driver.user.last_name} {self.driver.user.first_name} -- {days}, {time})'

In admin.py:
class DriverWorkDayAdmin(admin.ModelAdmin):
    list_display = ('driver', 'get_work_days', 'get_times')
    
    def get_work_days(self, obj):
        return obj.days
    
    ............

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question