Hi 

I hava a problem in django, i have to models one is a CustomUser Model and 
the other is a Address model. 

The problem is that when i create a generic CreateView for the Address 
model and use a pk: url the address form wont get the instance of the users 
PK for adding a new address.

my models look like this, Even though i pass the PK in the URL the 
CreateView does not understand that i want to add a address to user with 
id: 42 in this example. How can i solve this problem?







The URL
 path('<int:CustomUser_id>/address', views.AddressCreateView.as_view(), name
='Address-add'),


*This is my view*
class AddressCreateView(CreateView):
    model = Address
    template_name = 'members/address_form.html'
    success_url = reverse_lazy('MemberIndex')
    fields = ('CustomUser', 'address', 'zip_code', 'city', 'state','active')


*The model*
class CustomUser(AbstractBaseUser, PermissionsMixin):
    email = models.EmailField(_('email address'), unique=True)
    personal_Id = models.CharField(max_length=12, blank=False)
    first_name = models.CharField(_('first name'), max_length=30, blank=True
)
    middle_name = models.CharField('middle name', max_length=30, blank=True)
    last_name = models.CharField(_('last name'), max_length=30, blank=True)
    is_staff = models.BooleanField(default=False)
    is_active = models.BooleanField(default=True)
    date_joined = models.DateTimeField(default=timezone.now)
    avatar = models.ImageField(upload_to='images', blank=True)

    def __str__(self):
        return self.email

    def full_name(self):
        return '%s %s %s' % (self.first_name, self.middle_name, self
.last_name)

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = []


    objects = CustomUserManager()

class Address(models.Model):
    CustomUser = models.ForeignKey(CustomUser, on_delete=models.CASCADE)
    address = models.CharField(max_length=60, blank=False)
    zip_code = models.CharField(max_length=8, blank=False)
    city = models.CharField(max_length=30, blank=False)
    state = models.CharField(max_length=30, blank=False)
    active = models.BooleanField(name='active', default=True, editable=True)
    address_reg_date = models.DateTimeField(default=timezone.now)
    class Meta:
        verbose_name_plural = "Address"
 
    def __str__(self):
         return self.address +', '+ str(self.zip_code) + ', ' + self.city





-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/d0d3596f-6c0e-4928-8e57-7eb5476821d3o%40googlegroups.com.

Reply via email to