Django admin date time helpers missing - django

I have a model that includes a datetimefield...
# Create your models here.
class BroadcastNotification(models.Model):
message = models.TextField()
broadcast_on = models.DateTimeField()
sent = models.BooleanField(default=False)
class Meta:
ordering = ['-broadcast_on']
In the admin pages the helpers for that field have gone away.
I don't know what I did to cause it as I didn't test everything after every change. I did do collectstatic fairly recently if that might be the cause.

Related

ModelForm foreignkey field not loaded yet

I'm attempting to develop a work record tracker sort of app, very similar to a ticket tracker. Each 'ticket' can have multiple notes owned by employees (employees are tracked in a different app that has been installed into the ticket-tracker).
i.e.
MySite
|___WorkRecords
|__models.py
|__views.py
|__etc.. //standard app stuff
|___emp #the uncreative name for the employee manager app
|__ //standard stuff again...
Within the models:
WorkRecords.models.py
class WorkRecord(models.Model):
issuedate = models.DateField('date first issued',
editable = False, default = timezone.now)
owner = models.ForeignKey('emp.Employee', related_name='work_owner',
on_delete=models.PROTECT)
other_irrelevant_header_data...
....
class WorkRecordNote(models.Model):
ref_wr = models.ForeignKey(WorkRecord, on_delete=models.CASCADE)
note_text = models.CharField(max_length=1000)
author = models.ForeignKey('emp.Employee', related_name='note_author',
on_delete=models.PROTECT)
#Form class for the Work Record
class EditWR(ModelForm):
class Meta:
model = WorkRecord
fields = '__all__'
emp.models.py
class Employee(models.Model)
data stuff
At the moment, the models have all been migrated, but the data has not been input into the tables yet (MySQL tables are all there, matching models etc...but no data within tables yet)
Now that I've gotten the models and tried working out modelform, I've tried makemigrations, but I'm getting an error traceback...
ValueError: Cannot create form field for 'owner' yet, because its related model u'emp.Employee' has not been loaded yet
I checked my mysite/settings.py to ensure that the apps were installed, and the config files are all there:
...
INSTALLED_APPS = [
'WorkRecords.apps.WorkrecordsConfig',
'emp.apps.EmpConfig',
etc...
]
...
To me, this sounds like it can't create the form because there isn't any data 'loaded' into the database, but what else I've learned about Django suggests that it shouldn't care yet. Is that accurate? Or is there something else I'm missing?
EDIT:
I made a change to the model of WorkRecord that somewhat fixed the problem (at least the error isn't there anymore, but I don't know about side-effects)
class WorkRecord(models.Model):
...
owner=models.ForeignKey('emp.Employee', related_name='work_owner',
on_delete=models.PROTECT, editable=False) #Added the editable=False
...
All else stayed the same, but makemigrations did not throw an error this time. What's the reasoning here?
Try changing the order on your installed apps:
INSTALLED_APPS = [
emp.apps.EmpConfig, # first
WorkRecords.apps.WorkrecordsConfig,
etc...
]

Adding/deleting fields in the same ModelForm django

I have little experience in django so I would really appreciate your help!
In general, I have created a ModelForm which depending on the users who is logged in, he changes some values in the field. So consider that this form is being edited about 5 times by 5 different users.
I would like to show a specific field in the template (and view) only when the third user is logged in.
My model is :
class Task(models.Model):
Taskdetails = models.CharField(max_length=500, null=True)
asset = models.ForeignKey('Asset', null=True)
failure = models.ForeignKey('Failure', null=True)
cause = models.ForeignKey('Cause', null=True)
Created_task_date = models.DateTimeField(default=timezone.now, null=True)
employee = models.ForeignKey("auth.User", null = True)
and also i have created this ModelForm
class Meta:
model = Task
fields = ('Taskdetails', 'asset', 'failure', ,'employee','cause',)
Also I have 5 edititions of the TaskForm in which is user edits something.
The thing I am trying to do is to show the cause field only in the third form.
I tried to exclude the value but nothing apperas.
If i include the field cause (just like above), I must "pass" it in the template in order to be edited from the first user (otherwise the task_form is not saved)
I hope I became clear.
I would really appreciate your help.

Why is this giving me an MRO error?

I'm using Django 1.8rc1. When I try to do a makemigrations with this models.py:
from django.db import models
from datetime import datetime
class TrackedModel(models.Model):
created_date = models.DateField()
modified_date = models.DateField()
class Meta:
abstract = True
class Project(models.Model):
name = models.CharField(max_length=12)
due_date = models.DateField()
complete_date = models.DateField(default=datetime.now)
I am getting:
TypeError: Cannot create a consistent method resolution
order (MRO) for bases Model, TrackedModel
I can't even see where it would be getting confused over methods with such a simple abstract model. In case you're wondering, the Project model is inheriting from models.Model in the example but that was just to troubleshoot -- ultimately I want Project to inherit from TrackedModel.
What am I missing?
Figured it out. The migration history got me again. I had to clear out the files in my app's migrations folder. Apparently it had stored a previous set of models that were not set up right, during a previous migration.

Stacked Inline with many rows crashing in Django

When trying to navigate to 'Add a presentation' in Django admin, I have to wait ~1 minute for the response to render. The problem is that I have ~500 slides in the database and the admin is selecting all the slides three different times to fill in the menues. I am obviously doing something wrong with my model definitions, as I wouldn't expect this amount of data to bring my server to its knees. Any visibility into why I am experiencing this issue with the way I have defined relationships or am using the django admin?
class PresentationTitle(models.Model):
title = models.CharField(max_length=255)
order_number = models.IntegerField(default=0)
def __unicode__(self):
return self.title
class PresentationUser(models.Model):
user = models.OneToOneField(User)
authorized_modules = models.ManyToManyField(PresentationTitle)
class Presentation(models.Model):
title = models.ForeignKey(PresentationTitle)
user = models.ForeignKey(PresentationUser)
presentation_date = models.DateTimeField()
def __unicode__(self):
return self.title.title
class Slide(models.Model):
....
submodule = models.ForeignKey(Submodule)
presentation = models.ManyToManyField(Presentation, through='PresentationSlide')
...
class Meta:
order_with_respect_to = 'submodule'
ordering = ['order']
class PresentationSlide(models.Model):
presentation = models.ForeignKey(Presentation)
slide = models.ForeignKey(Slide)
slide_order = models.IntegerField()
Additionally, my admin contains:
class PresentationSlideInline(admin.StackedInline):
model = PresentationSlide
class PresentationAdmin(admin.ModelAdmin):
inlines = [PresentationSlideInline]
admin.site.register(Presentation, PresentationAdmin)
Understandably, removing just having PresentationAdmin from the admin.site.register makes it load very responsively.
Resolved my issue -- there was another culprit at play: django-debug-toolbar
After removing this from my setup, the admin panel for the Presentation effectively loads.
Thanks to everyone on the interest and the support, be wary of profiling add-ons when you are experience performance issues.

Django - transactions in the model?

Models (disregard typos / minor syntax issues. It's just pseudo-code):
class SecretModel(models.Model):
some_unique_field = models.CharField(max_length=25, unique=True) # Notice this is unique.
class MyModel(models.Model):
secret_model = models.OneToOneField(SecretModel, editable=False) # Not in the form
spam = models.CharField(max_length=15)
foo = models.IntegerField()
def clean(self):
SecretModel.objects.create(some_unique_field=self.spam)
Now if I go do this:
MyModel.objects.create(spam='john', foo='OOPS') # Obviously foo won't take "OOPS" as it's an IntegerField.
#.... ERROR HERE
MyModel.objects.create(spam='john', foo=5) # So I try again here.
#... IntegrityError because SecretModel with some_unique_field = 'john'
already exists.
I understand that I could put this into a view with a request transaction around it, but I want this to work in the Admin, and via an API, etc. Not just with forms, or views. How is it possible?
The title of the section may be "Controlling transaction management in views", but you should read the first Note box within the section.