0

I've a question. I need to add second functionality to may Django Administration panel:

  • I have 2 models (City and Region), one Region has many Cities in it;
  • In my admin panel I want to add excel file with cities list when I'm creating new Region

models.py

class City(models.Model):
    name = models.CharField('Город', max_length=50, unique=True)
    slug = models.SlugField(max_length=50, unique=False, blank=True, null=True)
    city_latt = models.CharField('Широта', max_length=50, blank=True, null=True)
    city_long = models.CharField('Долгота', max_length=50, blank=True, null=True)
    country = models.CharField('ISO код страны', max_length=5)

   class Meta:
        verbose_name = 'Город'
        verbose_name_plural = 'Города'
    
   def __str__(self):
     return self.name
class Canton(models.Model):
    """ Canton list """
    canton_name = models.CharField('Название', max_length=255, blank=False)
    canton_city = models.ManyToManyField(City, verbose_name='Города', blank=False)
    canton_code_for_map = models.CharField('Код для карты', max_length=20, null=True, blank=True, default=None)
    canton_shortify = models.CharField('ISO обозначение', max_length=3, null=True, blank=True)
    country = models.CharField('ISO код страны', max_length=5)
    slug = models.SlugField('url', max_length=255, unique=True, blank=True)

    def __str__(self):
        return self.canton_name

    def get_cities(self):
        return ", ".join([c.name for c in self.canton_city.all()])
    get_cities.short_description = "Города"
    get_cities.admin_order_field = 'canton_city'

    class Meta:
        verbose_name = 'Территориальные еденицы'
        verbose_name_plural = 'Территориальные еденицы'

so how I can create custom form in my admin where I can upload file and than assign specific Region to uploaded cities?

now my admin for Regions looks like this: admin.py

class CantonForm(forms.ModelForm):
    canton_city = forms.ModelMultipleChoiceField(queryset=City.objects.order_by('name'), label='Города')

    class Meta:
        model = City
        fields = '__all__'

class CantonAdmin(admin.ModelAdmin):
# class CantonAdmin(ImportMixin, admin.ModelAdmin):
    list_display = ('__str__', 'canton_name', 'canton_code_for_map', 'canton_shortify', 'slug', 'get_cities', 'country')
    list_filter = ('canton_name', 'country')
    search_fields = ['canton_name__icontains', 'canton_shortify__icontains', 'canton_city__name__icontains', 'country']
    form = CantonForm
    ordering = ['canton_name']

admin.site.register(Canton, CantonAdmin)

1 Answer 1

0

It's not easy to understand exactly what you mean. Can you provide more detail? A diagram or annotated screenshot would help.

If you want to have an extra form field in the import screen where you can assign the same region to each city in the upload, then this is possible, the docs have an example that you can copy.

However, if you want to be able to modify each city in the interface, this would be a lot more difficult.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.