Code often used in Python / Django apps [prefectures]

Background of this article

For example, in the case of an app that registers store information, I think that the prefecture of that store may be set.

I've left it in this article as a reminder so that you can use this feature at any time.

code

models.py



class Pref(models.Model):
    code = models.CharField(max_length=2)
    name = models.CharField(max_length=4)
    
    def __str__(self):
        return str(self.name)

views.py



def upload(request):
    if 'csv' in request.FILES:
        form_data = TextIOWrapper(request.FILES['csv'].file, encoding='shift_jis')
        csv_file = csv.reader(form_data)
        for line in csv_file:
            pref, created = Pref.objects.get_or_create(code=line[0])
            pref.code = line[0]
            pref.name = line[1]
            pref.save()

        return render(request, 'pref/upload.html')

    else:
        return render(request, 'pref/upload.html')

CSV file to import

image.png

that's all.

Recommended Posts

Code often used in Python / Django apps [prefectures]
A collection of code often used in personal Python
Techniques often used in python short coding (Notepad)
List of Python code used in big data analysis
A collection of Excel operations often used in Python
Settings often used in Jupyter
[Python] Frequently used library code
Python frequently used code snippets
Generate QR code in Python
Character code learned in Python
Commands often used in the development environment during Python implementation
I tried to summarize the code often used in Pandas
[Python] Generate QR code in memory
Automatically format Python code in Vim
[Python] Basic knowledge used in AtCoder
Grammar summary often used in pandas
Write selenium test code in python
Code tests around time in Python
Fourier series verification code written in Python
Summary of methods often used in pandas
Disk-related commands often used in Ubuntu (memories)
Get the EDINET code list in Python
Processing memos often used in pandas (beginners)
Notes on using code formatter in Python
Python scikit-learn A collection of predictive model tips often used in the field
List of main probability distributions used in machine learning and statistics and code in python
Python scikit-learn A collection of predictive model tips often used in the field
++ and-cannot be used for increment / decrement in python
Personal notes to doc Python code in Sphinx
[Django] Personally used commands, code templates, TDD [Memo]
Use Python in Anaconda environment with VS Code
Send push notifications to iOS apps in Python
Create initial settings and staff apps in Django
Settings for Python coding in Visual Studio Code
Standard .py file used in Python trials (template)-2020
Astro: Python modules / functions often used for analysis
Convert cubic mesh code to WKT in Python
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Models in Django
Python Django Tutorial (5)
Unittest in python
Epoch in Python
Sudoku in Python
DCI in Python
python character code
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Python Django Tutorial (8)
Python Django Tutorial (6)
Plink in Python
Constant in python
Lifegame in Python.