from oscar.apps.catalogue.abstract_models import AbstractProduct
from oscar.core.compat import AUTH_USER_MODEL
from django.db import models
class Product(AbstractProduct):
seller = models.ForeignKey(
AUTH_USER_MODEL,
on_delete=models.CASCADE,
null=True)
from oscar.apps.catalogue.models import *
I added this code to forked catalog model >
I want to show it in the dashboard,Image of dashboard and dropdown box I tried admin.site.register but it is not working.
This is the code for override of form , when I fork and overrtide it doesn't work but when I change the code in core it works .
from oscar.apps.dashboard.catalogue.forms import ProductForm
from oscar.core.loading import get_class, get_classes, get_model
from yourappsfolder.catalogue.models import Product
class SellerField(ProductForm):
class Meta(ProductForm.Meta):
model =Product
fields = [
'title','seller', 'upc', 'description', 'is_public', 'is_discountable', 'structure']
You have forked the form incorrectly. Calling your form class SellerField will not work. The form class needs to have exactly the same name as the core form, otherwise Oscar's loader will not find it. Change it like this:
from oscar.apps.dashboard.catalogue.forms import ProductForm as BaseProductForm
class ProductForm(BaseProductForm):
class Meta(BaseProductForm.Meta):
fields = ['title','seller', 'upc', 'description', 'is_public', 'is_discountable', 'structure']
Related
I have a model with the the below field:
Choice_options=(
('Open',Open'),
('Pending','Pending'),
('Re-verify','Re-verify'),
('Approved',Approved'),
('Cancelled',Cancelled'),
)
class Sample(models,model):
Choice=models.Charfield(Max_length=50,default='Open',choices=Choice_options)
When the superuser log in to this admin page, he should see only the following options in the choice field:
("Approved","Cancelled", Re-verify")
Thanks in advance,
You can define a ModelForm to specify how to edit the record:
# app/forms.py
from django import forms
from app.models import Sample
admin_choice_choices=(
('Pending', 'Pending'),
('Re-verify', 'Re-verify'),
('Approved', 'Approved'),
('Cancelled', 'Cancelled'),
)
class AdminSampleForm(forms.ModelForm):
Choices = forms.ChoiceField(<b>choices=admin_choice_choices</b>)
class Meta:
model = Sample
fields = '__all__'
In your admin.py you can then specify the form:
# app/admin.py
from django.contrib import admin
from app.forms import AdminSampleForm
from app.models import Sample
#admin.register(Sample)
class SampleAdmin(admin.ModelAdmin):
form = AdminSampleForm
Note: normally the name of the fields in a Django model are written in snake_case, not PerlCase, so it should be: choice instead of Choice.
Does anyone know, how to register child class derived from abstract class in admin.py (the abstract class is in file abstract_models.py file) I tried major solution in the web but does not seem to work. It is possible as pointed by various contributors but don't know what I am doing wrong!
My folder structure is like this
'''
gaasc/ <- project folder
contain urls.py, admin.py,models.py,etc
gaasc_apps/<- contains all apps
core/
abstract_models.py
models.py
admin.py,...
about_app/
models.py,
admin.py
urls.py
'''
I am trying to leverage abstract class inside core app to models inside about_app app. Yes, between different apps.
Steps followed:
create abstract_models.py and define abstract class
import it in core/models.py
3.import the abstract class from core/models.py inside about_app/models.py
register the class in about_app/models.py to admin.py(in about_app/)
abstract_models.py file(inside core/abstract_models.py) has
import uuid
from django.db import models
class AbstractTimeStampModel(models.Model):
"""TimeStampModel that holds created_date and updated_date field"""
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
created_date = models.DateTimeField("Created date", auto_now_add=True)
updated_date = models.DateTimeField("Updated date", auto_now=True)
def __str__(self):
return self.created_date
class Meta:
abstract = True
class AbstractTextAreaOnly(AbstractTimeStampModel):
"""Abstract class for textfield"""
about = models.TextField(
verbose_name="Description",
blank=True
)
def __str__(self):
return "Textonly class-"+self.id
class Meta:
ordering = ['created_date']
models.py in core/models.py
from django.db import models
from .abstract_models import (
AbstractTextAreaOnly,
)
Now I want to use this abstract class in about_app/
So my derived class in models.py inside about_app/models.py looks:
from django.db import models
from gaasc_apps.core.models import(
AbstractTextAreaOnly,
)
class GeneralInformation(AbstractTextAreaOnly):
'''
Gives general information of college
'''
class Meta():
db_table = "general_information"
verbose_name="General information"
ordering=['created_date']
What I tried:
I tried to registering using following ways:
method1:
in admin.py in about_app/admin.py:
from django.contrib import admin
from .models import (
GeneralInformation,
)
#register(GeneralInformation)
class GeneralInformationAdmin(admin.ModelAdmin):
pass
method 2:
in about_app/admin.py
from django.contrib import admin
from .models import (
GeneralInformation,
)
class GeneralInformationAdmin(admin.ModelAdmin):
pass
admin.site.register(GeneralInformation,GeneralInformationAdmin)
method3:
in about_app/admin.py
......
class GeneralInformationAdmin(admin.ModelAdmin):
readonly_fields = ('id','about','created_date','updated_date')
list_display=('id','about','created_date')
fieldsets=[
('Id',{'fields':['id']}),
('About',{'fields':['about']}),
('Created_Date',{'fields':['created_date']}),
]
admin.site.register(GeneralInformation,GeneralInformationAdmin)
With all this solution GeneralInformation is not shown in admin panal/dashboard of django.
The solution seems to be simple but I don't know why its not working?
Some solution I tried are here:
How to register inherited sub class in admin.py file in django?
Django admin model Inheritance is it possible?
Register abstract model in admin django 1.6
VERSION:
Django-2.2
python-3.6
I have found the solution to the problem , it was simple
we need to define config setting in each apps. Due to lack of config setting and perticular directory structure it was not able to read it.E.g.
in about_app/__init__.py :
default_app_config = "gaasc_apps.about_app.apps.AboutAppConfig"
and in about_app/apps.py we should have like this
from django.apps import AppConfig
class AboutAppConfig(AppConfig):
name = 'gaasc_apps.about_app'
verbose_name="about application"
def ready(self):
pass
I am using radio buttons for user input in forms.py and want to save the rated value in django database.I have the following fields:
from product.models import Rating
from django.forms import forms
from django.forms.fields import ChoiceField
from django.forms import ModelForm
from django import forms
class RatingForm(forms.ModelForm):
class Meta:
model = Rating
fields = ('product', 'user', 'rating')
widgets = forms.ChoiceField(widget=forms.RadioInput(),
required=True)
Model.py
class Rating(models.Model):
CHOICES = (
('5-stars', '5-stars'),
('4-stars', '4-stars'),
('3-stars', '3-stars'),
('2-stars', '2-stars'),
('1-stars', '1-stars'),
)
product=models.ForeignKey(Product,null=True,blank=True, on_delete=models.PROTECT)
user=models.ForeignKey(User,null=True,blank=True, on_delete=models.PROTECT)
rating=models.ChoiceField(choices=CHOICES, max_length=128)
I didn't find any library for importing this widget. Below is the error i am facing:
AttributeError: module 'django.forms' has no attribute 'RadioInput'?
Please if any one can help? Or suggest any other way to do this?
The widget is called RadioSelect, not RadioWidget. See the documentation.
Note however, you must use the widget directly in the widgets attribute, not as part of a field; and widgets is a dictionary of field names to widgets:
widgets = {'rating': forms.RadioSelect}
Hi guys I have this model
class Class1(models.Model):
....
ctime = models.FloatField() # I am storing date in timestamp here
....
Is it possible to display ctime field in admin panel not as float but as time field?
Check Django documentation. You can override default widgets for model fields -
from django.db import models
from django.contrib import admin
# Import our custom widget and our model from where they're defined
from myapp.widgets import RichTextEditorWidget
from myapp.models import MyModel
class MyModelAdmin(admin.ModelAdmin):
formfield_overrides = {
models.TextField: {'widget': RichTextEditorWidget},
}
Or, for a single model, as described in this answer -
class StopAdminForm(forms.ModelForm):
class Meta:
model = Stop
widgets = {
'approve_ts': ApproveStopWidget(),
}
class StopAdmin(admin.ModelAdmin):
form = StopAdminForm
Check other answers in that question too.a
I have an app name sync which has a form created from a model that saves itself. I want to create another app called activity that retrieves the data from the sync models and other future apps. How can I do that in the activity views app?
This is my sync models.py
from django.db import models
from django.contrib.auth.models import User
from django.forms import ModelForm
FS_CHOICES = (
('/path1/', 'P1'),
('/path2/', 'P2'),
('/path3/', 'P3'),
)
OPTIONS = (
('-n', 'TRY'),
)
class SyncJob(models.Model):
date = models.DateTimeField()
user = models.ForeignKey(User, unique=False)
source = models.CharField(max_length=3, choices=FS_CHOICES)
destination = models.CharField(max_length=3, choices=FS_CHOICES)
options = models.CharField(max_length=10, choices=OPTIONS)
class SyncJobForm(ModelForm):
class Meta:
model = SyncJob
fields = ['source', 'destination', 'options']
Ok, in activity views.py I have this:
from toolbox.sync.models import SyncJob
from django.shortcuts import render_to_response
def Activity()
sync_job = SyncJob.objects.get(id=03)
return render_to_response('base.html', {'sync_job': sync_job})
UPDATE: When I try to view the page it displays the error:
'function' object is not iterable
Just import it like any other python class.
So in your activity app you'd do something like this:
from sync.models import SyncJob
sync_job = SyncJob.objects.get(id=99)