Opened 11 years ago

Closed 11 years ago

#20753 closed Bug (duplicate)

Beginner confused for MultiValueDictKeyError, where the error comes from?

Reported by: adam.pahlevi@… Owned by: nobody
Component: contrib.admin Version: 1.4
Severity: Normal Keywords: MultiValueDictKeyError
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I am new to Django and would like to try it for my thesis project. However, I found an error which is MultiValueDictKeyError when I try to save my model from the Django admin.

I have 3 models in this case, which are: BSN (Business), LIN (Lines), ROT (Routes).

BSN:

class Bsn(models.Model):
    """The entity of business"""

    #the enum for business type
    BUSINESSTYPE = (
        ('bank', 'Bank'),
        ('barclub', 'Bar-Club'),
        ('carrental', 'Car rental'),
        ('church', 'Church'),
        ('clinic', 'Clinic'),
        ('cybercafe', 'Cybercafe'),
        ('exchange', 'Exchange'),
        ('festival', 'Festival'),
        ('fleamarket', 'Fleamarket'),
        ('hotel', 'Hotel'),
        ('mosque', 'Mosque'),
        ('police', 'Police'),
        ('primary', 'Primary'),
        ('restaurant', 'Restaurant'),
        ('secondary', 'Secondary'),
        ('shop', 'Shop'),
        ('sight', 'Sight'),
        ('transportation', 'Transportation'),
        ('uni', 'University')
    )

    bsnid = models.AutoField(primary_key=True)
    id = bsnid.__str__()
    bsnname = models.CharField("Business name", max_length=135, unique=True)
    bsntype = models.CharField("Business type", max_length=42, choices=BUSINESSTYPE)
    bsndesc = models.CharField("Description", max_length=135, blank=True)
    bsnparent = models.ForeignKey('self', null=True, db_column='bsnparent', blank=True)
    bsncoorx = models.DecimalField("Coordination X in Map", null=True, blank=True, max_digits=11, decimal_places=0)
    bsncoory = models.DecimalField("Coordination Y in Map", null=True, blank=True, max_digits=11, decimal_places=0)
    bsncoorn = models.DecimalField("Coordination N in Map", null=True, blank=True, max_digits=11, decimal_places=0)

    def __unicode__(self):
        return self.bsnname

    class Meta:
        db_table = u'bsn'
        verbose_name="Business"
        verbose_name_plural="Businesses"

Class LIN

class Lin(models.Model):
    """model for the line"""
    linid = models.AutoField(primary_key=True)
    id = str(linid)
    linbsn = models.ForeignKey(Bsn, db_column='linbsnid')

    def __unicode__(self):
        return "Line " + self.linbsn.bsnname

    class Meta:
        db_table = u'lin'
        verbose_name = "Line"

And, the last but not least, ROT:

class Rot(models.Model):
    rotid = models.IntegerField(primary_key=True)
    rotlin = models.ForeignKey(Lin, null=True, db_column='rotlinid', blank=True)
    rotfrom = models.ForeignKey(Trn, null=True, db_column='rotfrom', blank=True, related_name="rotfrom")
    rotto = models.ForeignKey(Trn, null=True, db_column='rotto', blank=True, related_name="rotto")
    rotkm = models.FloatField(null=True, blank=True)
    rottimemnt = models.IntegerField(null=True, blank=True)
    rotprice = models.FloatField(null=True, blank=True)
    rotisinterchange = models.BooleanField(default=False, null=False, blank=False, verbose_name="is interchange?")
    rotisactive = models.BooleanField(default=True, null=False, blank=False, verbose_name="is active?")
    rotdesc = models.CharField(max_length=225, blank=True)

    def __unicode__(self):
        if self.rotfrom is None:
            return "to " + str(self.rotto)
        elif self.rotto is None:
            return "from " + str(self.rotfrom)
        else:
            return "{0} to {1}".format(self.rotfrom.bsnid.bsnname, self.rotto.bsnid.bsnname)

    class Meta:
        db_table = u'rot'
        verbose_name = "Route"

The model are generated from inspectdb, and all is working when I try to save one by one manually; both from Django admin and from the Django console, and also from a Pilot.py file that I myself created to fill the database automatically whenever I want, with initial data.

But, when I go to admin page for LIN, that itself is a 'child' of BSN and that itself is used to determine and show the ROT; I got the multivalued key error. I have tried to understand this error, but I lost myself. I have no one at hand to help except than the Django community here. So, please let me know where I am wrong at.

Here are the Admin code.

class RotAdmin(admin.ModelAdmin):
    """admin page for route"""
    fieldsets = [
        (None, {'fields': ['rotlin', 'rotfrom', 'rotto', 'rotisinterchange', 'rotisactive']}),
        ("Distance", {'fields': ['rotkm', 'rottimemnt', 'rotprice']}),
        ("Description", {'fields': ['rotdesc']})
    ]
admin.site.register(Rot, RotAdmin)

class RotGridsTabular(admin.TabularInline):
    fields = ['rotfrom', 'rotto', 'rotkm', 'rottimemnt', 'rotprice', 'rotisinterchange', 'rotisactive']
    model = Rot
    extra = 0

class LinAdmin(admin.ModelAdmin):
    """admin page for line"""
    fields = ['linbsn']
    inlines = [RotGridsTabular]
admin.site.register(Lin, LinAdmin)

Note: when I add the ROT record from the ROT admin, it works; when I add the ROT record from LIN admin, it crashes and throws the Multivalued key error.

Attachments (1)

MultiValueDictKeyError at _admin_levi_lin_40_.htm (232.2 KB ) - added by adam.pahlevi@… 11 years ago.
The error that is produced

Download all attachments as: .zip

Change History (4)

by adam.pahlevi@…, 11 years ago

The error that is produced

comment:1 by Collin Anderson, 11 years ago

My guess is there's a Rot that's should be showing on /admin/levi/lin/40/, but it's not showing up, possibly because of bad data. Try getting Lin.objects.get(pk=40).rot_set.all()[0], and seeing if there's any bad or strange data for that Rot object.

Also, for best results, try upgrading to django 1.5, because it may be that the error you are running into has been fixed.

in reply to:  1 comment:2 by anonymous, 11 years ago

Replying to CollinAnderson:

My guess is there's a Rot that's should be showing on /admin/levi/lin/40/, but it's not showing up, possibly because of bad data. Try getting Lin.objects.get(pk=40).rot_set.all()[0], and seeing if there's any bad or strange data for that Rot object.

Also, for best results, try upgrading to django 1.5, because it may be that the error you are running into has been fixed.

Okay I will try your suggestion. Thanks.

For that particular reason as of why I am not using Django 1.5 is because I need MySQL, because the data also consumed by Java program, I don't want to switch to Postgres and change the driver and anything it is, I don't use ORM as I like SQL for better performance. So, unless I can use MySQL in Django 1.5 or Python 3.3 or the latest, I will stick with the old version unfortunately enough.

Thanks for the dedication to check the problem. And I will report it soon afterwards.

comment:3 by Karen Tracey, 11 years ago

Resolution: duplicate
Status: newclosed

I believe the underlying problem here is same as #13696

Note: See TracTickets for help on using tickets.
Back to Top