You could use InlineFormSet, which is designed specially to handle such problems. Refer to the documentations here https://docs.djangoproject.com/en/3.1/topics/forms/modelforms/#inline-formsets
Here you could provide the primary model (in your case it will be CUstomUser), and the secondary model (which will be Address). Something similar to below. from django.forms import inlineformset_factoryCustomerFormSet = inlineformset_factory(CustomUser, Address, fields=('email', 'firstname', ...... 'address','zipcode',....)) Hope this helps. Thanks & Regards, --------------------- Mayank Tripathi "Do what you can, with what you have, where you are -by Theodore Roosevelt" https://datascience.foundation/datascienceawards On Tue, Jun 9, 2020 at 7:36 AM The Sha <mahj...@gmail.com> wrote: > 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 > <https://groups.google.com/d/msgid/django-users/d0d3596f-6c0e-4928-8e57-7eb5476821d3o%40googlegroups.com?utm_medium=email&utm_source=footer> > . > -- 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/CALQo0B8n1%2B%3D9LzLBDdb-VeJjUc7vW92Cxjo4fc6bGzuPnHwRGw%40mail.gmail.com.