0

I'm trying to create an address form with multiple address, where the user can choose home or shipping address. I have the current model:

from django.db import models
from django.contrib.auth.models import User
from PIL import Image


class Address(models.Model):
    name = models.CharField(max_length=30)
    address = models.CharField(max_length=50)
    city = models.CharField(max_length=60, default="Miami")
    state = models.CharField(max_length=30, default="Florida")
    zipcode = models.CharField(max_length=5, default="33165")
    country = models.CharField(max_length=50)

    class Meta:
        verbose_name = 'Address'
        verbose_name_plural = 'Address'

    def __str__(self):
        return self.name

So I was wondering if that's correct.

Anyway, I was wondering how with the current model I can create a view so I can have the address form. Using a normal model would be "easy" but how can I do it using the through option in the model?

Could someone lend me a hand please?

Thank you

Arturo
  • 3,254
  • 2
  • 22
  • 61

3 Answers3

0

use a foreign key to point to your address model:

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    nick_name = models.CharField('Nick name', max_length=30, blank=True, default='')
    bio = models.TextField(max_length=500, blank=True)
    image = models.ImageField(default='default.jpg', upload_to='profile_pics')
    addresses = models.ForeignKey(Address) # <-- fix here

Hope this helps!

Michael Artman
  • 319
  • 1
  • 14
  • Thanks for the reply, it says: `TypeError: __init__() missing 1 required positional argument: 'on_delete'` on that line, so I modified it like: `addresses = models.ForeignKey('Address', on_delete=models.DO_NOTHING)` and when I delete an address, it says: FOREIGN KEY constraint failed. I think something is not right there :) – Arturo Feb 27 '19 at 20:43
  • Try using the `on_delete=models.CASCADE`. I'm not fully sure on this point but I believe you cannot delete the address because the foreign key in the profile model cannot point to a null address causing integrity issues in the database. [More info](https://stackoverflow.com/questions/38388423/what-does-on-delete-do-on-django-models). – Michael Artman Feb 28 '19 at 14:37
0

You should declare ForeignKey with '<app>.<model>' format:

class AddressType(models.Model):   
    address = models.ForeignKey('yourapp.Address', on_delete=models.CASCADE)
    profile = models.ForeignKey('yourapp.Profile', on_delete=models.CASCADE)

or directly give the class:

    address = models.ForeignKey(Address, on_delete=models.CASCADE)
    profile = models.ForeignKey(Profile, on_delete=models.CASCADE)
Zulu
  • 8,765
  • 9
  • 49
  • 56
  • Ohhh so that's how you declare it... nice! But I still have the same error. I updated it on the top post – Arturo Feb 27 '19 at 20:50
0

Both of the other answers were incorrect, I ended up modifying everything and also creating a new model, here it is:

class Address(models.Model):
    name = models.CharField(max_length=100, blank=False)
    address1 = models.CharField("Address lines 1", max_length=128)
    address2 = models.CharField("Address lines 2", max_length=128, blank=True)
    city = models.CharField("City", max_length=64)
    # state = USStateField("State", default='FL')
    state = models.CharField("State", max_length=128, default='FL')
    zipcode = models.CharField("Zipcode", max_length=5)
    user = models.ForeignKey(Profile, on_delete=models.CASCADE, blank=False)

    class Meta:
        verbose_name_plural = 'Address'

    def __str__(self):
        return self.name
Arturo
  • 3,254
  • 2
  • 22
  • 61