Learning Django
Learning Django

Reputation: 265

One model object per user Django

I don't want to extend my userprofile.

I made a new model with name, contact and email.

The problem :

With createview the user is able to create multiple instances of the user_info model.

Is there any chance we can limit user to make only one user_info and update the same everytime.

models.py

class user_info(models.Model):
    booked_by = models.CharField(max_length=100)
    Name = models.CharField(max_length=40)
    contact = models.IntegerField()
    email = models.EmailField()

views.py

class user_info_create(LoginRequiredMixin,CreateView):
    login_url = 'Mel:user_login'
    form_class = user_infoform
    template_name = 'Mel/user_info_form.html'

    def form_valid(self, form):
        form.instance.booked_by = self.request.user
        return super(user_info_create, self).form_valid(form)



class user_info_detail(LoginRequiredMixin,DetailView):
    login_url = 'Mel:user_login'
    model = user_info
    context_object_name = "book"

    def get_queryset(self):
        return user_info.objects.filter(booked_by=self.request.user)

Upvotes: 0

Views: 970

Answers (3)

Learning Django
Learning Django

Reputation: 265

Thank you vorujack and e4c5.

Just by doing OneToOneField is not solving the entire problem.

The below code really solved my problem.

If its not correct or can be done in a better way please let me know.

model.py

booked_by = models.OneToOneField(User)

views.py

class user_RedirectView(LoginRequiredMixin,RedirectView):
        def get_redirect_url(self):
             if user_info.objects.filter(booked_by=self.request.user).exists():
                  return reverse('Mel:user_update')
             else:
                  return reverse('Mel:user_info_create')

Upvotes: 0

vorujack
vorujack

Reputation: 1950

you can make a relation to your user model like this:

class user_info(models.Model):
    booked_by = models.OneToOneField(User)
    Name = models.CharField(max_length=40)
    contact = models.IntegerField()
    email = models.EmailField()

on create view set booked_by field with current user. with this change if user want to create multiple user_info it raised exception and no user_info inserted

Upvotes: 1

e4c5
e4c5

Reputation: 53774

As mentioned by @vorujack You need to create a OneToOne relationship between user_info and your user model. The R and RDBMS stands for Relations. So you need to build relationships between models. At the moment your system doesn't have any relation between user and the profile. However, the correct syntax is

booked_by = models.OneToOneField(User)

Then you need to do

python manage.py makemigrations
python manage.py migrate

if you already have duplicate entries in the table, the second step will fail. In that case you need to clear out the duplicates and run it again. If you have invalid entries in that table, the migration will still fail. So if you don't have any critical data, you might in fact want to clear out the whole table before you do this.

Another point worth noting. https://legacy.python.org/dev/peps/pep-0008/#id39

Class Names Class names should normally use the CapWords convention.

The naming convention for functions may be used instead in cases where the >interface is documented and used primarily as a callable.

Note that there is a separate convention for builtin names: most builtin names are single words (or two words run together), with the CapWords convention used only for exception names and builtin constants.

So your classes should really be UserInfo and UserInfoCreate

Upvotes: 2

Related Questions