I have table on my page like that:
And models.py like that:
models.py
class Book(models.Model):
title = models.CharField(max_length=100)
author = models.CharField(max_length=30)
category = models.IntegerField()
offset_pages = models.IntegerField()
read_pages = models.IntegerField()
total_pages = models.IntegerField()
book_path = models.CharField(max_length=200, default='')
status = models.BooleanField(default=False)
def __str__(self):
return f'{self.category} | {self.title} | {self.author}'
I would like to click on value in "Read pages" column (404 in this case) and then modify it and save to database.
The only way I see it for now is to make an html input there with assigned value of 404, then change it within an input and submit it with additional button.
I know how to do so, but it seems to me like there has to be way better solution.
Does any onemay present me another solution?
You can use JavaScript for that. You can define an onclick attribute for the read pages values and when the user clicks it, you can replace the text with an input field whose value attribute should be dynamically set to the value of the text.
The input field can have a small "ok" button next to it, which when pressed have to use an ajax call to update the the database and changing the displayed value without reloading the page.
Related
I am new to django. I have a model like this:
class Standup(models.Model):
team = models.ForeignKey("Team", on_delete=models.CASCADE)
standup_time = models.DateTimeField(auto_now_add=True)
class StandupUpdate(models.Model):
standup = models.ForeignKey("Standup", on_delete=models.CASCADE)
employee = models.ForeignKey("Employee", on_delete=models.CASCADE)
update_time = models.DateTimeField(auto_now_add=True)
status = models.CharField(max_length=50)
work_done_yesterday = models.TextField()
work_to_do = models.TextField()
blockers = models.TextField()
If I write view for this model, every employee will have to hit API for his/her standup update. But I am supposed create a single API which takes updates of all the employees and saves it into database. In frontend, it will be something like this:
Employee will select on a team as one employee can be a part of
multiple teams.
Then the employee will give his/her stadup updates.
Then another employee will do the same thing and so on.
At the end,by clicking on submit button, whole data will be saved together.
Any guidance on how to do it?
Not sure why you need a separate model for Updates.
I would try to approach it like that:
make the Standup model reference both Team and Employee models;
last_update, status, work_to_do etc. as its fields;
make a custom serializer that accepts a list with Standup field values and takes the authorized user's ID from request object's data. last_update time can be now(), status calculated according to your business logic
This part of DRF documentation could probably be helpful.
The following is in my models.py:
class SensorType(models.Model):
hardware_type = models.CharField(max_length=100)
is_static = models.BooleanField(default=False)
# Some other fields
class Sensor(models.Model):
device_id = models.CharField(max_length=100, primary_key=True)
sensor_type = models.ForeignKey(SensorType, on_delete=models.PROTECT)
# Some other fields
class Asset(models.Model):
name = models.CharField(max_length=100)
sensor_type = models.ForeignKey(SensorType, on_delete=models.PROTECT) # I need to use this field to filter below
sensor = models.ForeignKey(Sensor, on_delete=models.PROTECT, limit_choices_to={'sensor_type': WHAT DO I PUT HERE?},)
# Some other fields
I need to limit the choices in the sensor field of asset so that only sensors with the sensor_type set in the field immediately above, show up.
The reasoning behind this is that there will eventually be many sensors and it would be very useful to filter this. Initially I only need this to work from the admin page but this will eventually extend when I make my Create and Update Views.
Is this even possible? I'm essentially trying to access attributes before the object has actually been created.
After reading several other questions such as this one I have also looked into ModelChoiceField but the same issue exists of trying to access the form data before it has been submitted.
I'm very open to changing the model structure if that is what is required.
I have 2 models:
class Tag(models.Model):
"""
model for tag
"""
# name of tag
name = models.CharField(max_length=30, verbose_name="tag name", unique=True)
def __str__(self):
return self.name
class Note(models.Model):
"""
model for note
"""
# date of publication note
publication_date = models.DateField("Publication date", auto_now_add=True)
# date of last change
date_last_change = models.DateField("Last changed", auto_now=True)
# note title
title = models.CharField(max_length=200, verbose_name="Note title")
# main text of note
main_text = models.TextField("Note text")
# tags
tags = models.ManyToManyField(Tag, related_name='Tags')
def __str__(self):
return self.title
As you see, one note has many tags, and one tag is related to many notes. But when I started to fill data in admin panel, I noticed that when I add a tag in note it automatically binds to all notes. Even if I add tag with Tag menu in admin panel it also binds to all notes.
I already tried manually adding model like NoteTag with foreign keys to Note and Tag but have the same issue. I still new in django and little bit confused by this situation.
Are you sure about this? Is it possible you've get confused by default django ManyToManyField widget showing all related records?
Try to check it with shell this way:
note = Note.objects.get(id=1)
note.tags.all()
will print all note tags.
And you can tweak widgets with third-party apps (https://pypi.python.org/pypi/django-widget-tweaks/1.3) But i guess this is not so simple :)
So I want to make habit tracking web page, I have 3 models (habits are called category)
class Day(models.Model):
name = models.CharField(max_length=8)
date = models.DateField()
class Category(models.Model):
name = models.CharField(max_length=100)
categoryBD = models.ManyToManyField(Day, through='CategoryByDay')
class CategoryByDay(models.Model):
day = models.ForeignKey(Day)
category = models.ForeignKey(Category)
status = models.CharField(max_length=64)
As you can see it's rather simple. To get track of what I did this specific day I connect each category with each new day.
Now I managed to put some neat buttons next to each category and even got jquery magic to make ajax request when each button is clicked and to hide each button and update status. However, when I refresh the page I still get all the buttons.
So I need to check in the template, if the category this day has a created CategoryByDay model, and if the status is set to 0 or 1.
Can I do it throught template syntax?
It would require some strange querying. I pass to my template today's date and list of categories. Maybe I should expand the list of categories to add a status here?
I don't understand why you need a name for your Day class, but anyway all three classes can be done in one simple class:
class Category(models.Model):
name = models.CharField(max_length=100)
date = models.DateField()
status = models.BooleanField()
Next you want to check what have been done at specific date? Simply make a query:
categorybyday = Category.objects.filter(date=your_date)
I'm trying my usual way of getting more human friendly output by using get_XX_display() where XX is the field in question. This normally works for CharFields but I'm now moving to using ManyToManyField but I cannot get the same response. How could I parse the options in a 'nice' way even though I may have multiple options, I simply want the text part.
I use this for displaying my checkboxes, so my options are usually of the following form:
MY_CHOICES = (
('_LS','A'),
('_HS','B'),
('_SS','C'),
('_NS','D'),
('_ES','E'),
)
So I'd like to say return 'A' if that option has been selected or 'A' 'B' if those two have been selected. I can get values with say: .values_list() but cannot get just the human readable text, not the '_LS' etc etc. Is there a smart way to get it out without looping through and concatenating the string.
The model is:
class M(models.Model):
MyFID = models.ForeignKey(A)
MyManyField = models.ManyToManyField(Choices)
class Choices(models.Model):
slug = models.CharField(primary_key=True, max_length=32)
title = models.CharField(unique=True, max_length=64)
def __str__(self):
return self.title