Why do I get such an error only for PermiLayer model , when the same code/logic works perfectly fine for other models .
My code is this
uperm = PermiLayer()
perml=uperm.objects.get(pk = num)
Ps: I have already tried Manager isn't accessible via `Model` instances solutions there . But didn;'t work out
Edit:
This is my models
from django.contrib.gis.db import models
from django.contrib.auth.models import User
from django.contrib.contenttypes.models import ContentType
from maps.models import Sdr_Layer
from OsmMap.models import OsmLayers
class PermiLayer(models.Model):
user = models.ForeignKey(User)
table = models.IntegerField()
permi = models.IntegerField()
Managers are accessible only via Model Classes, rather than from model instances
https://docs.djangoproject.com/en/dev/topics/db/queries/#retrieving-objects
why don't you just get it like
perml=PermiLayer.objects.get(pk = num)
Related
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')
After following various examples here on stackoverflow my new field isn't showing up on django oscar dashboard here are my models and dashboard fork.
catalogue
models.py
from django.db import models
from oscar.apps.catalogue.abstract_models import AbstractProduct
class Product(AbstractProduct):
file = models.FileField(upload_to='files/%Y/%m/%d')
from oscar.apps.catalogue.models import *
dashboard/catalogue/forms.py
from oscar.apps.dashboard.catalogue import forms as base_forms
class ProductForm(base_forms.ProductForm):
class Meta(base_forms.ProductForm.Meta):
fields = ('file',)
I am wondering what i did wrong for people that have faced this issue before I need help. thanks.
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, ...)
given the following situation:
models.py
from .managers import PersonManager
from django.db import models
class Person(models.Model):
first_name = models.CharField(max_length=256)
last_name = models.CharField(max_length=256)
managers.py
from .models import Person
from django.db import managers
class PersonManager(models.Manager):
def create(self, person_dict):
new_person = Person(
first_name=person_dict['first_name']
last_name=person_dict['last_name'])
new_person.save()
How can I write my model manager to avoid circular import?
It is actually not working, my guess is that I would have to create my object inside my manager without refering to it as class Person, instead I should use a more general generic Django name. Any thoughts?
There are a few options here.
Firstly, you could define the model and the manager in the same file; Python has no requirement or expectation that each class is in its own file.
Secondly, you don't actually need to import the model into the manager. Managers belong to models, not the other way round; from within the manager, you can refer to the model class via self.model.
And finally, if that's all your manager is doing, there is no reason for it at all. Managers already have a create method; it takes keyword parameters, rather than a dict, but that just means you can call it with Person.objects.create(**person_dict).
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)