How to assign a default choice value to a user when they sign up in django framework

I am writing a logic where when a users creates and account, i want to automatically assign the free membership to them and i know this should be done in the register view but i don’t know why it’s not working as expected. I still have to manually go to my admin page and manually assign a value to newly created user and that’s not what i really wanted.

class Membership(models.Model):
        ('Enterprise', 'Enterprise'), # Note that they are all capitalize//
        ('Team', 'Team'),
        ('Student', 'Student'),
        ('Free', 'Free')
        ('Days', 'Days'),
        ('Week', 'Week'),
        ('Months', 'Months'),
    slug = models.SlugField(null=True, blank=True)
    membership_type = models.CharField(choices=MEMBERSHIP_CHOICES, default='Free', max_length=30)
    duration = models.PositiveIntegerField(default=7)
    duration_period = models.CharField(max_length=100, default='Day', choices=PERIOD_DURATION)
    price = models.DecimalField(max_digits=10, decimal_places=2, default=0.00)

    def __str__(self):
       return self.membership_type

#### User Membership
class UserMembership(models.Model):
    user = models.OneToOneField(User, related_name='user_membership', on_delete=models.CASCADE)
    membership = models.ForeignKey(Membership, related_name='user_membership', on_delete=models.SET_NULL, null=True)
    reference_code = models.CharField(max_length=100, default='', blank=True)

    def __str__(self):
       return self.user.username

@receiver(post_save, sender=UserMembership)
def create_subscription(sender, instance, *args, **kwargs):
    if instance:
        Subscription.objects.create(user_membership=instance, + timedelta(days=instance.membership.duration))

def register(request):
    reviews = Review.objects.filter(status='published')
    info = Announcements.objects.all()
    categories = Category.objects.all()
    if request.method == "POST":
        form = UserRegisterForm(request.POST)
        if form.is_valid():
            username = form.cleaned_data.get('username')
            obj = request.user
            get_membership = Membership.objects.get(membership_type='Free')
            instance = UserMembership.objects.create(user=obj, membership=get_membership)
            messages.success(request, f'Account Successfully created for {username}! You can Login In Now')
            return redirect('userauths:login')
    elif request.user.is_authenticated:
        return redirect('elements:home')
        form = UserRegisterForm()
    context = {
        'reviews': reviews,
        'form': form,
        'info': info,
        'categories': categories
    return render(request, 'userauths/register.html', context)

Hi desphix,

If you’re seeing the message.success response, I would start by inspecting what obj, get_membership and instance all are. If something’s not working as you expect, those three are the initial suspects as to why the UserMembership isn’t being created as you expect it to.

Additionally, what does UserRegisterForm do on Is that creating a user? If it is, I’m not 100% sure that will also update request.user. But then again, I would have expected creating the UserMembership instance with an anonymous user to fail.

You’ll need to fix the tabs and spaces inconsistencies in what you copied over and what exists.

i have checked thorughly and everythin seems fine, even the identations but it shows the red line errors, please is there anything i can do about it

If you put your cursor in the white space on the lines that have red lines and those that don’t, then go left or right, you’ll see some span a greater width than others. Those that are spanning a greater width are likely the tab characters. The others are space characters. You need to decide which you want to use, then replace the tabs or spaces with spaces or tabs.

Personally, I use spaces and then configure my IDE to use 4 spaces when I press the tab key.

okay thanks alot
let me try it out

Great! thanks for sharing.