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
Related
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)
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, ...)
I am trying to fork django-oscar to change the dashboard form for product attributes, multioption. Need to have a description field for every option.
project/oscar_fork/catalogue/models.py:
from django.db import models
from django.utils.translation import ugettext_lazy as _
from oscar.apps.catalogue.abstract_models import AbstractAttributeOption, AbstractAttributeOptionGroup
class AbstractAttributeOption(AbstractAttributeOption):
description = models.CharField(_('Description'), max_length=250, blank=True)
group = models.ForeignKey(
'catalogue.AttributeOptionGroup',
on_delete=models.CASCADE,
related_name='optionsblabla',
verbose_name=_("Group"))
from oscar.apps.catalogue.models import *
The models are changed with the extra field "description" in my DB, but still my form field returns cannot find this field.
project/oscar_fork/dashboard/catalogue/forms.py:
from oscar.apps.dashboard.catalogue import forms as base_forms
class AttributeOptionForm(base_forms.AttributeOptionForm):
class Meta(base_forms.AttributeOptionForm.Meta):
fields = ('option', 'description')
If I change the form.py and models.py fields directly in the Oscar app it works. Other forms can be forked that easily as shown above. Tried it with AttributeOptionGroupForm. I think there is a problem with the sequence of import. How can I solve this?
Error:
django.core.exceptions.FieldError: Unknown field(s) (description) specified for AttributeOption
I am using django-oscar v1.6. Django v.2.08.
Your concrete model should be named AttributeOption without the 'Abstract', otherwise oscar will not pick it up and use its own AttributeOption model instead, which has no description:
class AttributeOption(AbstractAttributeOption):
description = ...
You will have to run makemigrations and migrate after that.
Check the source code of the models module that you import at the end. You will see how their dynamic model loading works:
if not is_model_registered('catalogue', 'AttributeOption'):
class AttributeOption(AbstractAttributeOption):
pass
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.
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)