0

I'm getting the following error message (see below) when trying to list my products in the Django admin. However I have defined qr_code as you can see.

ImproperlyConfigured at /admin/products/product/ 'ProductAdmin.fieldsets[4][1]['fields']' refers to field 'qr_code' that is missing from the form.

models.py

class Product(models.Model):
    title = models.CharField(max_length=60)
    qr_url = models.URLField(blank=True)
    qr_image = models.ImageField(
        upload_to="public/uploads/",
        height_field="qr_image_height",
        width_field="qr_image_width",
        null=True,
        blank=True,
        editable=False
    )
    qr_image_height = models.PositiveIntegerField(null=True, blank=True, editable=False)
    qr_image_width = models.PositiveIntegerField(null=True, blank=True, editable=False)


    def __unicode__(self):
        return self.title

    def qr_code(self):
        return '' % self.qr_url
    qr_code.allow_tags = True

admin.py

from django.contrib import admin
from models import Product

class ProductAdmin(admin.ModelAdmin):

    list_display = ['title']
    fieldsets = (
        (None, {
            'fields': ('title', 'description', 'active')
        }),
        ('QR Code', {
            'classes': ('collapse',),
            'fields': ('qr_url', 'qr_code')
        }),

    )

admin.site.register(Product, ProductAdmin)
jason
  • 2,895
  • 8
  • 26
  • 38

2 Answers2

2

qr_code cannot be referenced as a method on a model in a form. It has to be defined as a model field or form field if you intend to use it in a form within a model admin.

Brandon Taylor
  • 33,823
  • 15
  • 104
  • 144
  • any examples of this I can look at? – jason Feb 21 '13 at 14:34
  • Not to be rude, but, maybe the Django documentation? What are you trying to accomplish? Your method for `qr_code` doesn't make any sense - it just returns a string - the `qr_url` property. – Brandon Taylor Feb 21 '13 at 14:38
  • 1
    I'm just learning and following this example: http://www.acedevs.com/blog/2011/07/25/quick-qr-codes-django/ – jason Feb 21 '13 at 14:43
  • No worries. That's a fairly complicated example for a beginner. Basically, Django expects that a field on a ModelForm directly correlates to a column in a database table. – Brandon Taylor Feb 21 '13 at 14:48
  • but whats confusing me is that all the examples I see where you try to add an image to the admin seem to just call the method not a direct field . whats the difference between this here: http://stackoverflow.com/questions/2443752/django-display-image-in-admin-interface and what I'm doing? To me it looks like he is calling a model method too. confused :( I'm try to add the image to the admin but calling the method in the same way. – jason Feb 21 '13 at 14:56
  • 1
    The difference there is that the method is being used in the `list_display` in that ModelAdmin, not referenced as an input in the change form. – Brandon Taylor Feb 21 '13 at 14:59
  • Good deal. I would highly recommend checking out "Practical Django Projects". Of all the books available, this one helped me out the most when learning Django. – Brandon Taylor Feb 21 '13 at 15:15
1

Add qr_code to readonly_fields in ProductAdmin.

readonly_fields = ('qr_code')
Nazik
  • 8,696
  • 27
  • 77
  • 123