django ModelForm: self: Unable to get repr for <class

578 views Asked by At

I have a django from that is working in my development environment, but not my production environment.

If I debug my development environment, I notice this warning appear by the form's __init__ function:

self: Unable to get repr for <class 'badges.forms.BadgeAssertionForm'> args <class 'tuple': (None,) kwargs: {'initial': {'badge': <Badge: Test Badge>}}

forms.py

class BadgeAssertionForm(forms.ModelForm):
    class Meta:
        model = BadgeAssertion
        # fields = '__all__'
        exclude = ['ordinal', 'issued_by', 'semester']

    def __init__(self, *args, **kwds):
        super(BadgeAssertionForm, self).__init__(*args, **kwds)
        self.fields['user'].queryset = User.objects.order_by('profile__first_name', 'username')
        self.fields['user'].label_from_instance = lambda obj: "%s (%s)" % (obj.profile, obj.username)

The form is created in views.py:

def assertion_create(request, user_id, badge_id):
    # view can accept a user_id or badge_id, otherwise they will be 0
    initial = {}
    if int(user_id) > 0:
        user = get_object_or_404(User, pk=user_id)
        initial['user'] = user
    if int(badge_id) > 0:
        badge = get_object_or_404(Badge, pk=badge_id)
        initial['badge'] = badge

    form = BadgeAssertionForm(request.POST or None, initial=initial)

The other answers I've seen all suggest adding a __str__ function to the class, however, I've never seen a ModelForm requiring a __str__ before? Also, when I add one the error still appears.

The problem in my production environment is that the User field doesn't appear in the form at all. Just the Badge field.

Here's the BadgeAssertion in models.py

class BadgeAssertion(models.Model):
    badge = models.ForeignKey(Badge)
    user = models.ForeignKey(settings.AUTH_USER_MODEL)
    #... some other fields.

Edit: I believe this stopped working when I upgraded Django to 1.11 (from 1.9?), but I'm not positive.

0

There are 0 answers