django-models

Create template object to display dependent on time of day

I need to create a cross-site template object which will check the current time and return either one string if it's within a time range specified in a model or another or blank in all other cases. Seems simple, but I wonder what is the best approach here? Also, there are a few other considerations: the string should be editable the d...

Django - Overriding get_or_create with models.py

Hi all, I have a class in which I want to override the get_or_create method. Basically if my class doesn't store the answer I want it do some process to get the answer and it's not provided. The method is really a get_or_retrieve method. So here's the class: class P4User(models.Model): user = models.CharField(max_length=100, ...

User-specific model in Django

I have a model containing items, which has many different fields. There is another model which assigns a set of this field to each user using a m2m-relation. I want to achieve, that in the end, every user has access to a defined set of fields of the item model, and he only sees these field in views, he can only edit these field etc. Is ...

Admin interface editable Django app settings

Is there a good way provide user configurable app settings in Django admin? Basically I would like to have a nice forms where site owner can easily edit such one off information as his contact information, front page text content, etc. Sort of like a normal admin interface of a model, but limited to only one undeletable item in the mode...

Programmatically saving image to Django ImageField

Ok, I've tried about near everything and I cannot get this to work. I have a Django model with an ImageField on it I have code that downloads an image via HTTP (tested and works) The image is saved directly into the 'upload_to' folder (the upload_to being the one that is set on the ImageField) All I need to do is associate the already ...

Models duplicating on save instead of updating

I'm trying to make a Django application to handle events. The view below handles the editing of already created events. @login_required def event_admin(request, event_id): event = get_object_or_404(Event, pk=event_id) if request.method == 'POST' and request.user == event.organiser: event_form = EventAdminForm(request.PO...

django unique for each date

Hello First of all let me show you my model: class ChannelStatus(models.Model): channel = models.ForeignKey(Channel,unique_for_date="date") date = models.DateField() platform = models.ManyToManyField(Platform) Right now, when I want to make an ChannelStatus entry, there cant be more than one entry which is t...

Many files upload in ImageField form - django problem

Hi! My problem is simple. I have template like this: <form enctype="multipart/form-data" action="{% url offers.views.add_offer %}" method="post"> <input type="file" name="image1" /> <input type="file" name="image2" /> <input type="submit" value="Add" /> </form> Model looks like that: class Image(models.Model): ...

Many to many lookups in Django

This is probably insultingly simple and worthy of a Nelson Muntz laugh, but I'm having a real braindead moment tryng to make many to many connections across various model relationships. I have the following models (simplified for your enjoyment!): class Document(models.Model): title = models.CharField(max_length=200) author = m...

How to select all objects not referenced in a many-to-many relationship

I have a couple of Django models set up like this: class Group(models.model): name = models.CharField(max_length=50, unique=True) class Section(models.Model): name = models.CharField(max_length=50, unique=True) slug = models.SlugField(help_text='Auto generated') groups = models.ManyToManyField(Group, blank=True) In on...

How can I optimize my database queries using my actual django model ?

I can't post image because I'm new so here's a link of what I want. So, I have the model on the left and I want the view on the right. As of now, I'm looping over every thread I'm interested in. Template code: {% for thread in threadlist %} {% for post in thread.postlist %} ... Model code: class Thread (models.Model): ... def po...

Django Admin - Bulk editing data?

Are there any admin extensions to let bulk editing data in Django Admin? (ie. Changing the picture fields of all product models at once. Note that this is needed for a users POV so scripting doesn't count.) Any thoughts on subject welcome. ...

save() method deletes existing record when using unique_together in Django model

I am using the following class in models.py: class Player(models.Model): id = models.AutoField(primary_key=True) sport = models.CharField(max_length=3, choices=SPORT_CHOICES) yid = models.IntegerField() first = models.CharField(max_length=30) last = models.CharField(max_length=30) team = models.CharField(max_leng...

Django models - Does django cache former queries?

Below I call the same methods in author and w5 model objects. But one of them raises an error: >>> author = models.Author('ali') >>> author.article_set.count() --------------------------------------------- ValueError: invalid literal for int() with base 10: 'ali' >>> w5 = models.Author(name='w5') >>> w5.article_set.count() 0 Actually...

django template system, calling a function inside a model

hello, I want to call a function from my model at a template such as: class ChannelStatus(models.Model): .............................. .............................. def get_related_deltas(self,epk): mystring = "" if not self.get_error_code_delta(epk): return mystring else: for i in self.get_listof_ou...

How to implement Synchronization Tag in django

We are making a synchronization between one master database and many slave databases over tight link (7kb). To minimize amount of data send we tag each record that was sent successfully. To do so we created following models: class SynchronizationTag(models.Model): STATUSES = ((0, "Invalid"), (1, "Pending"), ...

Problems updating user profiles.

Working out editing profiles and ran into a little trouble! The following piece of code succesfully updates the mug_shot column in the user profile, but also erases all the other column data for that particular record. It's weird because Django is supposed to automatically distinguish between updates/saves. What's weirder is that everywh...

Why does django enforce all model classes to be in models.py?

I have just learned that splitting model classes into different files breaks many of django's built-in functionalities. I am coming from a java background. There, it is not accepted as a good practice writing very long class files. But django's enforcement of single file for all model classes will probably cause programmer to write ver...

How to add bi-directional manytomanyfields in django admin?

In my models.py i have something like: class LocationGroup(models.Model): name = models.CharField(max_length=200) class Report(models.Model): name = models.CharField(max_length=200) locationgroups = models.ManyToManyField(LocationGroup) admin.py (standard): admin.site.register(LocationGroup) admin.site.register(Report) ...

Django empty field fallback

Hi there, I have a model that holds user address. This model has to have first_name and last_name fields since one would like to set address to a recipient (like his company etc.). What I'm trying to achieve is: if the fist_name/last_name field in the address is filled - return simply that field if the fist_name/last_name field in the...