B
B
Boldy2014-11-08 02:22:01
Django
Boldy, 2014-11-08 02:22:01

How to fix bad request when creating a model in django?

When you click on the plus sign next to the foreign key, a new window opens with the following url: 127.0.0.1:8000/admin/address/address/add/?_to_fiel... and inside bad request. If you remove _to_field=id& or at least id - the form opens normally.
models.py

# -*- coding: utf-8 -*-g
from __future__ import unicode_literals
from django.db import models


class Address(models.Model):
    country = models.CharField(verbose_name='Страна', max_length=40)
    district = models.CharField(verbose_name='Область', max_length=40)
    postal_code = models.CharField(verbose_name='Почтовый индекс', max_length=40)
    city = models.CharField(verbose_name='Город', max_length=40)
    street = models.CharField(verbose_name='Улица/Микрорайон', max_length=40)
    building_number = models.CharField(verbose_name='Дом', max_length=40)
    apartment = models.CharField(verbose_name='Квартира', max_length=40, blank=True)

    class Meta:
        db_table = 'addresses'

    def __str__(self):
        return self.id

admin.py
from django.contrib import admin
from models import Address

class AddressAdmin(admin.ModelAdmin):
    pass

admin.site.register(Address, AddressAdmin)

Model with the same foreign key
class Client(models.Model):

    # при попытке добавить адрес через зелёный плюсик возле одного из этих полей - вылетает Bad Request 400
    passport_address = models.ForeignKey(Address, related_name='+')
    filter_address = models.ForeignKey(Address, null=True, blank=True, related_name='+')

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander, 2014-11-08
@Boldy

Your related_name has the same name. Make two different names. For this should also swear.
In general, in addition to Bad Request 400, something else is written in the error log, there should be an answer.

A
Alexey Bukin, 2014-11-08
@abukin

The plus sign is needed in order not to create feedback in the model and not for naming related_name.
If a connection is needed, then it's better to make a normal name for her human.
all named strings explicitly need to define unicode like this:
verbose_name=u'Город'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question