Django DJStripe access fields from foreign key in template - django

I have a user model with a foreign key that points to Subscription which is imported from DJ Stripe.
class User(PermissionsMixin, AbstractBaseUser):
...
subscription = models.ForeignKey(Subscription, on_delete=models.SET_NULL)
...
The Subscription table in djstripe has a subscription field and a created field which I am trying to access through my template like this:
{{ user.subscription.subscription }} or {{ user.subscription.created }}
I have tried several variations with double underscores or subscription_set.get but I am really just scratching my head. Is there a simple way to access those fields in my template?

Related

How to access table which has its one to one relations?

Sorry, I don't know what should be the title for this question. But here is my question.
In Django we User table comes as default.
Now I have another table User_Info which has User id as (one-to-one/foreign key),
I don't know what to take one-to-one or foreignkey.
Now I want to access this User_Info table which has enrollment through the User table in HTML. (Is it possible?)
example:-
class User_info(models.Model):
mobileNo = models.BigIntegerField(blank=True)
User_id = models.OneToOneField(User, on_delete=models.CASCADE)
we have User class as default and has primary key, username, email.
<h1> Name {{ user.username }} </h1> <!-- we can have username stored in -->
<h1> mobileNo {{ user.(i need help here, what to write)</h1> <!-- from user how to get mobile number-->
If I am missing anything else please ask.
Thank you:)
I forget to mention that the user has already been logged in so I can have the user table access in HTML.
Also 1 user has only 1 mobile number.
Rename your Model class to be according to Django coding style
class UserInfo(models.Model):
mobile_number = models.BigIntegerField(blank=True)
user = models.OneToOneField(User, on_delete=models.CASCADE)
One-to-one relationship docs
user.user_info.mobile_number

Django Social Auth - Referring to usersocialauth for foreign key in model

I'm using the social_django package for the first time. The project only requires authentication for the application through Facebook so I implemented the SOCIAL_AUTH_FACEBOOK method into my app, which retrieves basic info like username, first name, last name, email address. I also get the user's Facebook profile picture through SOCIAL_AUTH_FACEBOOK_EXTRA_DATA.
I want to figure out how to refer to the usersocialauth model as a parent key in my model. Right now, it's associating everything to auth_user model, which doesn't allow me to access the Facebook profile picture in the template.
Here is what I am doing at the moment:
models.py
from social_django import models as oauth_models
class Review(models.Model):
...
author = models.ForeignKey(oauth_models.USER_MODEL,
on_delete=models.CASCADE)
I am able to access the basic fields in the template through <p>{{ review.author.name }}</p>, but to see the profile picture, I have to do something like this:
{% for author in backends.associated %}
<img src="{{ author.extra_data.picture.data.url }}" alt="">
{% endfor %}
I would like to simplify this and just refer to the usersocialauth model so I can access all the data from one place in the template.
Found an easier solution.
I can keep ForeignKey relationship with the regular user model and use author.social_auth.all.0.extra_data.picture.data.url in the template.

Related name not working for django many to many field django

I am trying to access the value of an object that holds the ManyToMany relationship, similar to a related name for a ForeignKey or a OneToOne.
How do I need to change my models to allow this?
This is my current models.py
from django.db import models
from django.contrib.auth.models import User
class Account(models.Model):
account_owner = models.OneToOneField(User, on_delete=models.CASCADE, related_name='account_owner')
account_name = models.CharField(max_length=50)
members = models.ManyToManyField(User, related_name='account_profile')
When I try {{ request.user.account_owner.account_name }} I get the expected value of the account name (if they are the owner).
But when I try {{ request.user.account_profile.account_name }} I get nothing.
Unlike ForeignKey or a OneToOne, M2M returns a list.
So, I guessing you're trying to achieve this in your template.
Here is how you can
{% for member in request.user.account_profile.all %}
{{ member.account_name }}
{% endfor %}

Django OneToOneField show up on ModelForm of other entity?

I have the following classes:
class Account(models.Model):
instance = models.OneToOneField(Instance, blank=True, null=True)
class Instance(models.Model):
is_ready = models.BooleanField(default=False, verbose_name=_('Ready?'))
Right now the relationship between Account and Instance is set by a ModelForm for Account via Account.instance.
I would like to be able to set Account.instance via a ModelForm on Instance. Is that possible?
A ModelForm is:
a helper class that lets you create a Form class from a Django model.
An idea would be to write a custom model Field to address this issue. However, according to the relevant chapter of Writing Custom Model Fields
Fields in a model must somehow be converted to fit into an existing database column type.
Trying to spot the available postgreSQL column types I found this page which does not seem to provide a solution.
On the other hand, there is an old but updated SO post, which gives a solution that actually renders both forms in an HTML page and -this way- you gather all the needed fields in one form.
forms.py
class AccountForm(forms.ModelForm):
... Do stuff if necessary ...
class Meta:
model = Account
fields = ('the_fields', 'you_want')
class InstanceForm (forms.ModelForm):
... Do other stuff if necessary ...
class Meta:
model = Instance
fields = ('the_fields', 'you_want')
views.py
...
def register(request):
if request.method == 'POST':
account_form = AccountForm(request.POST)
instance_form = InstanceForm(request.POST)
if account_form.is_valid() and instance_form.is_valid():
account_form.save()
instance_form.save()
...
the_template.html
<form action="." method="post">
{% csrf_token %}
{{ account_form.as_p }}
{{ instance_form.as_p }}
<input type="submit" value="Submit">
</form>
This is also the idea of the admin's class InlineModelAdmin.
In the admin, for example, Account will become an inline model to Instance (which is a bad name for a django class).
admin.py:
from django.contrib import admin
class InstanceInline(admin.TabularInline):
model = Instance
class AccountAdmin(admin.ModelAdmin):
inlines = [
InstanceInline,
]

How to store social usernames/links in Django?

I have a personal blog written with Django. I want to make my social links/usernames dynamic so I can change them whenever I want.
I'm doing this with charfields. I'm adding new charfield for each social username/link field then my template's getting these usernames to show them in a tags. It's okay but I think it's not the easiest way.
Am I wrong or there is a better way to do this?
Maybe it would be easier to create a model for it. That way you would be able to add/remove social links without having to touch the code.
from django.conf import settings
from django.db import models
class SocialProfile(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='social_profiles')
network = models.CharField(max_length=100)
username = models.CharField(max_length=100)
url = models.URLField(max_length=500)
Then if you are using Django Admin, you could add this model as a TabularInline of the User model for example.
In your templates you could render the links dynamically:
{% for profile in request.user.social_profiles.all %}
{{ profile.username }}
{% endfor %}
Something like that.