1

I'm very new at Django. I have a custom user model:
accounts\models.py:

from django.contrib.auth.models import AbstractUser
from django.db import models
from tradestats.models import Webhook


class CustomUser(AbstractUser):
    webhook = models.ManyToManyField(Webhook, blank=True)

I have a tradestats app with a Webhook model:
tradestats\models.py

from django.db import models


class Webhook(models.Model):
    id = models.UUIDField(
        primary_key=True,
        default=uuid.uuid4,
        editable=False,
    )
    name = models.CharField(verbose_name='Webhooks name', max_length=50, null=False, blank=False)
    url = models.URLField(max_length=255, null=False, blank=False, unique=True)

    def __str__(self):
        return self.name

    class Meta:
        ordering = ['name']

I have a class based View. In this View I like to query the logged in user all Webhooks url field and pass to render() as a context.
tradestats\view.py:

           context = {
                'form': form.cleaned_data,
                'variable': value1,
                'variable2': value2,
            }
            return render(request, template_name='renko/renko_calculate.html', context=context)

How can I query the webhook.urls and pass into context?

Sunderam Dubey
  • 1
  • 11
  • 20
  • 40
Laszlo
  • 13
  • 4

2 Answers2

0

Try the following:

user = CustomUser.objects.get(id=request.user.id)
context = {
   ...         
   'webhooks': user.webhook
}
...

or just,

context = {
   ...         
   'webhooks': request.user.webhook.all()
}
...

It's all there in the documentation


Another way is to pass user data to the template and access it from there.

user = CustomUser.objects.get(id=request.user.id)
context = {
   ...         
   'user': user # needs to filter the content
}

# can use the request.user approach too

return render(request, template_name='renko/renko_calculate.html', context=context)

in template,

{% for wh in user.webhook.all %}
    {{ wh.id }} 
    {{ wh.name }} 
    {{ wh.url }} 
{% endfor %} 

Note: I didn't check this but needs to work

Avishka Dambawinna
  • 1,180
  • 1
  • 13
  • 29
0

I tried, but get this error:

'CustomUser' object has no attribute 'webhook'

I installed the debug_toolbar and examine the SQL query.

SELECT "accounts_customuser"."id",
       "accounts_customuser"."password",
       "accounts_customuser"."last_login",
       "accounts_customuser"."is_superuser",
       "accounts_customuser"."username",
       "accounts_customuser"."first_name",
       "accounts_customuser"."last_name",
       "accounts_customuser"."email",
       "accounts_customuser"."is_staff",
       "accounts_customuser"."is_active",
       "accounts_customuser"."date_joined"
  FROM "accounts_customuser"
 WHERE "accounts_customuser"."id" = 1
 LIMIT 21

I didn't see the webhook field lookup.

I tried with shell:

python manage.py shell
Python 3.10.5 (main, Jun  7 2022, 19:23:05) [GCC 11.2.1 20220219] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from accounts.models import CustomUser
>>> from tradestats.models import Webhook
>>> CustomUser.objects.get(id=1).username
'admin'
CustomUser.objects.get(id=1).webhook
Traceback (most recent call last):
  File "<console>", line 1, in <module>
AttributeError: 'CustomUser' object has no attribute 'webhook'

Update

It was my fault. webhook isn't exist but webhooks.
This works:

user = CustomUser.objects.get(id=request.user.id)
context = {
        'webhooks': user.webhooks.all(),
    }

    return render(request, template_name='tradestats/webhook.html', context=context)

I add .all() at the end of your code to iterate in template.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Laszlo
  • 13
  • 4