TransWikia.com

Django - No column found for custom field?

Stack Overflow Asked by jare42 on July 29, 2020

I’m having an issue creating a custom field for a user in Django currently. Ideally, I would want to create a custom field for a "CustomProgressBar" class that updates depending on the parameter "donations" passed into it and should be different for each user. Whenever I try to access an account on the server it returns "django.db.utils.OperationalError: table donorportal_profile has no column named progress". I have tried multiple times to makemigrations, migrate, and syncdb. I think there is an issue where I am not converting it to/from the database a certain way. I’m also unsure what to put in the parameters when I create the field in the model class. Any code relevant to my issue is attached below. I’m new to Django, so thank you for any help you can provide me!

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    city = models.CharField(max_length=40, default='')
    state = models.CharField(max_length=2, default='')
    website = models.URLField(default='')
    phone = models.IntegerField(default=0)
    progress = user_assets.ProgressField()
class CustomProgressBar:
    def __init__(self, donations):
        self.money = donations
        if 0 <= donations < 25:
            self.tier = 0
            self.percent = donations / 25
        elif 25 <= donations < 250:
            self.tier = 1
            self.percent = donations / 250
        elif 250 <= donations < 2500:
            self.tier = 2
            self.percent = donations / 2500
        elif donations >= 2500:
            self.tier = 3
            self.percent = 100
        else:
            self.tier = 0
            self.percent = 0
def parse_progress_bar(progress_bar_string):
    return CustomProgressBar(progress_bar_string)
class ProgressField(models.Field):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

    def deconstruct(self):
        name, path, args, kwargs = super().deconstruct()
        return name, path, args, kwargs

    def from_db_value(self, value, expression, connection):
        if value is None:
            return value
        return parse_progress_bar(value)

    def to_python(self, value):
        if isinstance(value, CustomProgressBar):
            return value

        if value is None:
            return value

        return parse_progress_bar(value)

    def get_prep_value(self, value):
        return value

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP