Django - using intermediate user group model - django

I need to use the intermediate model between user and group in another model like this:
Class SomeModel(models.Model):
usergroup = models.ForeignKey(UserGroupIntermediate, on_delete=models.DO_NOTHINH)
That is possible?
Thanks!
EDIT!:
My model looks like this, i'm using a custom user model:
from django.db import models
from django.contrib.auth import get_user_model
from model_utils.models import TimeStampedModel
class Enrollment(TimeStampedModel):
usergroups = models.ForeignKey(
get_user_model().groups.through,
but, this return an error when execute makemigrations
enrollments.Enrollment.usergroups: (fields.E300) Field defines a relation with model 'User_groups', which is either not installed, or is abstract.

You can refer to the model via the through attribute of the field.
usergroup = models.ForeignKey(User.groups.through, ...)

Related

List of contacts per User when extending AbstractUser

I want to create a custom user model with some extra fields, among which a contact list of other users. I would like to extend AbstractUser instead of creating a new model with a one-to-one link to User.
from django.contrib.auth import get_user_model
from django.contrib.auth.models import AbstractUser
from django.db import models
class CustomUser(AbstractUser):
a_custom_field = models.IntegerField(default=0)
# per-user contact list
contacts = models.ManyToManyField(get_user_model())
This code doesn't work. Throwing this error during makemigrations.
django.core.exceptions.ImproperlyConfigured: AUTH_USER_MODEL refers to model 'users.CustomUser' that has not been installed
The error totally makes sense, but what's the right way to achieve this?
I found the solution just by digging more into the django docs.
The problem is that I cannot use get_user_model() before the user model has been created.
The solution is using the class name as a string. So, this code works great:
from django.contrib.auth import get_user_model
from django.contrib.auth.models import AbstractUser
from django.db import models
class CustomUser(AbstractUser):
a_custom_field = models.IntegerField(default=0)
# per-user contact list
contacts = models.ManyToManyField('CustomUser')

Django Admin: How to show and hide fields on the basis of an app?

Here is the current scenario:
I have a model Transaction in the app "payment"
I want to add paypal_xyz_field in that model Transaction, once the new app payment_paypal is being installed.
ie: Extend Transaction in app paypal and add field paypal_xyz_field
What will be the approach?
If I will extend the Transaction model then I will have to show both paypalTransaction and Transaction in the admin.
In admin, I want to show only one model "Transaction"
with paypal_xyz_field, paymill_xyz_field, stripe_xyz_field not paypalTransaction, stripeTransaction etc.
Also, want to auto add/remove the field from DB/admin on the basis of apps
# payment/models.py
from django.db import models
class Payment(models.Model):
base_field = models.CharField(max_length=64)
# paypal/models.py
from django.db import models
from payment.models import Payment
class Payment(Payment):
paypal_specific_field = models.CharField(max_length=64)
# stripe/models.py
from django.db import models
from payment.models import Payment
class Payment(Payment):
stripe_specific_field = models.CharField(max_length=64)
add the editable attribute in model field and set it False.
from django.db import models
class Payment(models.Model):
base_field = models.CharField(max_length=64)

ManyToOne relationship with User database

I want to create a model with a ManyToOne relationship with the user database.
Here's the code I used for the field:
from django.db import models
from django.contrib.auth.models import User
class user_extended(models.Model):
user_ID = models.ManyToOneRel(User)
The migration doesn't work. It returns TypeError:
TypeError: init() missing 1 required positional argument: 'field_name'
How should I create a relationship with user database?
We define ManyToOne relationships on Django with a ForeingKey. So you should change
user_ID = models.ManyToOneRel(User)
to
user_ID = models.ForeignKey(User, on_delete=models.CASCADE)
Check out Django's documentation: https://docs.djangoproject.com/en/2.1/topics/db/examples/many_to_one/
If you want to have a ManytoOne relation (many user_extended to one User), you'd do it like this:
from django.db import models
from django.conf import settings
class user_extended(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
Note: the class name should be CamelCase, like this: UserExtended

Django, how to ForeignKey(auth_user)?

I am a newbie of django. I want to make a ForeignKey to the auth_user table.
I try to do that:
user = models.ForeignKey(auth_user)
It cause the error:
NameError: name 'auth_user' is not defined
So, can somebody tell me how to import auth_user.
Since foreign keys accept strings, you can use the AUTH_USER_MODEL setting in your foreign key.
from django.conf import settings
class MyModel(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL)
This works whether you are using the built in User model, or a custom model.
Presumably you are talking about django.contrib.auth.models.User then do
from django.contrib.auth.models import User
class MyModel(models.model):
user = models.ForeignKey(User)
If you are talking about a custom User model that comes from elsewhere, replace the import with the relevent import for that class.

django - Joining LogEntry to actual models

so i'm using the admin LogEntry object/table to log events in my app. I have a view where i'd like to display each LogEntry.
It would be really great if i could join the LogEntry with the actual objects they represent (so i can display attributes of the object inline with the log entry)
In theory this should be easy as we have the model type and id from the LogEntry but i can't figure out how to join them using a queryset.
i thought i could just grab all the ids of the different objects and make another dictionary for each object type and then join them somehow (maybe zip the lists together?) but that seems dumb and not very djano-ish/pythonic.
does anybody have better suggestions?
** edit **
just want to clarify am not looking to use admin, but roll a custom view and template.
As I know Django uses contenttypes framework to perform logging in admin. So you should create generic relation inside your model and then to show inlines in admin use GenericTabularInline and GenericStackedInline. Please consult with the article.
from django.contrib import admin
from django.contrib.admin.models import LogEntry
from django.contrib.contenttypes.generic import GenericTabularInline
from django import forms
from some_app import models
from some_app.models import Item
class LogForm(forms.ModelForm):
class Meta:
model = LogEntry
class LogInline(GenericTabularInline):
ct_field = 'content_type'
ct_fk_field = 'object_id'
model = LogEntry
extra = 0
class ItemForm(forms.ModelForm):
class Meta:
model = Item
class ItemAdmin(admin.ModelAdmin):
form = ItemForm
inlines = [LogInline,]
admin.site.register(models.Item, ItemAdmin)
and you add to Item:
class Item(models.Model):
name = models.CharField(max_length=100)
logs = generic.GenericRelation(LogEntry)
this change won't create anything in your database, so there is no need to sync
Recent Django versions require to create a proxy for LogEntry:
from django.contrib import admin
from django.contrib.admin.models import LogEntry
from django.contrib.contenttypes.generic import GenericTabularInline
class LogEntryProxy(LogEntry):
content_object = GenericForeignKey('content_type', 'object_id')
class Meta:
proxy = True
class LogInline(GenericTabularInline):
model = LogEntry
extra = 0
class ItemAdmin(admin.ModelAdmin):
inlines = [LogInline,]
admin.site.register(models.Item, ItemAdmin)