Valera
Valera

Reputation: 49

Django how to make model for user model

I'm developing to-list app with registration . I have two models : Model for User and Model for Tasks . I add new task throw Ajax to one user it adding and displaying for every user. Is there any solutions ? Here some pictures First user dashboard

Second user dashboard

Here is my code: models.py

 class Task(models.Model):
    title=models.IntegerField()
    date = models.DateTimeField(default=datetime.now,blank=True)
    is_published=models.BooleanField(default=True)

 class CustomUser(models.Model):
    user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    image=models.FileField(upload_to='photos/%Y/%m/%d/',null=True,blank=True)

views.py

  if request.method == 'POST' and request.POST['form_type'] == 'task':
    if request.is_ajax():
        addtask = AddTask(request.POST)
        if addtask.is_valid():
            user = request.user.id
            addtask.objects.filter(user=user).cleaned_data
            addtask.objects.filter(user=user).save()               
            task_object = Task.objects.filter(user=user)(addtask)
            return JsonResponse({'error': False, 'data': task_object})
        else:
            print(addtask.errors)
            return JsonResponse({'error': True, 'data': addtask.errors})
    else:
        error = {
            'message': 'Error, must be an Ajax call.'
        }
        return JsonResponse(error, content_type="application/json")



    addtask = AddTask()
    task = Task.objects.order_by('-date').filter(is_published=True)

html page

       {% if task %}
                    {% for tas in task %}
                Task content



                        {% endfor %}
                        {% else %}

                        {% endif %}

Upvotes: 0

Views: 53

Answers (3)

AgentDaun
AgentDaun

Reputation: 53

  1. If you want to create CustomUser model, you should create a class and inherit it from AbstractBaseUser or AbstractUser(django documentation).

  2. Your Task model hasn't relationships with CustomUser. You create AddTask(?) instance but didn't bind it with any user.

  3. You did not submit a view which renders HTML template, but I think that your query is something like Tasks = Task.objects.all() which return all tasks.

This is how you should create CustomUser model

This is documentation about relationships in Django

This is about making queries in Django

Upvotes: 0

Andrey Leontyev
Andrey Leontyev

Reputation: 451

Maybe you should add relation to CustomUser in Task model and filter tasks by owner in view before to render data to template?

 class Task(models.Model):
    title=models.IntegerField()
    date = models.DateTimeField(default=datetime.now,blank=True)
    is_published=models.BooleanField(default=True)
    user=models.ForeignKey(CustomUser)

 class CustomUser(models.Model):
    user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    image=models.FileField(upload_to='photos/%Y/%m/%d/',null=True,blank=True)

And in view:

    ...
    addtask = AddTask()
    task = Task.objects.filter(is_published=True, user_id=request.user.id).order_by('-date')

Upvotes: 1

calceamenta
calceamenta

Reputation: 41

So the mistake is that you never connected your CustomUser model with your Task model. They should have a relationship like one to many. Once that is achieved, you have to retrieve only the tasks related to the user of interest from the database and send them to the HTML page. Then only the tasks related to one particular user will be displayed.

Upvotes: 0

Related Questions