Hi all,
I am new to working with signals and triggers, what I am trying to accomplish is:
- When a new user registers, I have another model to deal with subscriptions.
- I want to create an instance for that subscription model with all the default values based on the new user.
- Otherwise no object is created in this model and it has to be done via a form.
Would appreciate any help and if any other code is needed let me know.
See code below:
Form part of user registration:
class ClubInfoForm(forms.ModelForm):
club_address2 = forms.CharField(required=False)
club_address3 = forms.CharField(required=False)
class Meta():
model = ClubInfo
fields = ('club_name', 'club_logo', 'club_address1', 'club_address2',
'club_address3', 'club_town', 'club_county', 'club_country',)
def clean_club_name(self):
club_name = self.cleaned_data['club_name']
if ClubInfo.objects.filter(club_name=club_name).exists():
raise ValidationError(_("Club already exists"))
return club_name
Views for registration:
def register(request):
registered = False
if request.method == 'POST':
user_form = UserForm(data=request.POST)
profile_form = ClubInfoForm(data=request.POST)
if user_form.is_valid() and profile_form.is_valid():
user = user_form.save()
user.set_password(user.password)
user.save()
profile = profile_form.save(commit=False)
profile.user = user
if 'profile_pic' in request.FILES:
print('found it')
profile.profile_pic = request.FILES['profile_pic']
profile.save()
registered = True
else:
print(user_form.errors, profile_form.errors)
else:
user_form = UserForm()
profile_form = ClubInfoForm()
return render(request,
'registration.html',
{'user_form': user_form,
'profile_form': profile_form,
'registered': registered})
Models for club info used upon registration and the model I want to update when a new user is created:
class ClubInfo(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
club_name = models.CharField(max_length=50, default='', unique=True)
club_logo = models.ImageField(upload_to='profile_pics', blank=True)
club_address1 = models.CharField(max_length=30)
club_address2 = models.CharField(max_length=30, default='')
club_address3 = models.CharField(max_length=30, default='')
club_town = models.CharField(max_length=30)
club_county = models.CharField(max_length=30)
club_country = models.CharField(max_length=30)
created_date = models.DateTimeField(default=timezone.now)
def set_default_packages(sender, **kwargs):
if kwargs['created']:
ClubPackages.objects.create(club_id=kwargs['instance'])
post_save.connect(set_default_packages, sender=club_name)
def __str__(self):
return self.club_name
class ClubPackages(models.Model):
club_id = models.ForeignKey(ClubInfo, on_delete=models.CASCADE)
PACKAGE_STATUS = (
('0', 'Active'),
('1', 'Not Active')
)
player_register_package = models.CharField(default='1', max_length=1, choices=PACKAGE_STATUS)
player_register_price = models.DecimalField(default=100.00, max_digits=8, decimal_places=2)
player_register_expiry = models.DateField(default=timezone.now)
roster_package = models.CharField(default='1', max_length=1, choices=PACKAGE_STATUS)
roster_price = models.DecimalField(default=50.00, max_digits=8, decimal_places=2)
roster_expiry = models.DateField(default=timezone.now)
rent_a_pitch_package = models.CharField(default='1', max_length=1, choices=PACKAGE_STATUS)
rent_a_pitch_price = models.DecimalField(default=100.00, max_digits=8, decimal_places=2)
rent_a_pitch_expiry = models.DateField(default=timezone.now)
shop_package = models.CharField(default='1', max_length=1, choices=PACKAGE_STATUS)
shop_price = models.DecimalField(default=50.00, max_digits=8, decimal_places=2)
shop_expiry = models.DateField(default=timezone.now).
--Séanadh Ríomhphoist/
Email Disclaimer
Tá an ríomhphost seo agus aon chomhad a sheoltar leis faoi rún agus is lena úsáid ag an seolaí agus sin amháin é. Is féidir tuilleadh a léamh anseo.
This e-mail and any files transmitted with it are confidential and are intended solely for use by the addressee. Read more here.
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAHZR7JdDb4-KO_28REy0wvDNxAaQk7qbQ0zn2uNmx9GN7T-KdA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment