bayman

0

votes
0

answer
11

views

Return a list of modified list pairs

Given my_list below with a list of list pairs, how do I create a new list where common element[0] values would have a list for their respective element[1] values? The order does not matter of elements from the main list does not matter. Example my_list = [['color', 'red'], ['length', 2], ['color', '...
bayman
1

votes
2

answer
130

views

How to structure requests exception handling and check for 200 response?

How should I structure the code so I won't get this error: UnboundLocalError: local variable 'r' referenced before assignment. If I want to ensure I get a 200 response before returning r.json(), where should I place this code — inside or outside the try block? if r.status_code == requests.code['ok...
bayman
1

votes
2

answer
36

views

How to switch decimal and rounded whole number using javascript

I have a table that renders data from my Django app which uses PostgreSQL database. How would I add a button to switch the Score column so it'll display the numbers between its original decimal value and rounded whole numbers? Here is an example of what it would look like: https://jsfiddle.net/8a6...
bayman
1

votes
1

answer
93

views

django username restriction list with django-allauth

I am using django-allauth for user registration and I would like to forbid users from creating specific usernames from a list[]. The list would include names from my url patterns, admin, root, profanity, etc. What's the best way to do this?
bayman
1

votes
1

answer
171

views

How to convert pandas DataFrame to json for django model?

I have the Pandas DataFrame below and need to convert it to json format with the df.columns wrapped in a Django model name and 'fields' fieldname. See below example of how the json should look. How should I do this? df: +-----------------+---------+------+ | Artist | Title | Year | +---...
bayman
1

votes
2

answer
37

views

How to replace all string in all columns using pandas?

In pandas, how do I replace & with '&' from all columns where & could be in any position in a string? For example, in column Title if there is a value 'Good & bad', how do I replace it with 'Good & bad'?
bayman
0

votes
1

answer
12

views

Shuffling keys and values in a list of dictionaries in python

Given data as a list with dictionaries, how would I return a new list of dictionaries where the values from the key 'color' become the new keys and the associated values from key 'id' becomes an element of a list for the new values? Example: data = [{'id': 123, 'color': 'red'}, {'id': 242, 'color':...
bayman
0

votes
1

answer
20

views

How to convert html special characters to different format in python?

I am retrieving a querystring from an API but I need it in another format for a different API. How can I use python3 to convert querystring1 so it'll be in the format given in querystring2? What character format is querystring1 and querystring2 in? They both should decode to 'q=score:>=100'. quer...
bayman
1

votes
1

answer
1.2k

views

How to allow user to delete account in django allauth?

I am trying to figure out the best way to allow users to delete or deactivate their account using django-allauth and I'm assuming I need to set is_active field to False. My solution below has a few issues: 1) The rendered checkbox field is_active shows the text 'Designates whether this user should...
bayman
2

votes
1

answer
206

views

Django ValueError Cannot assign “'testing'”: “Profile.artist” must be a “Artist” instance

I have created a form and have set the foreign key 'artist' as a CharField so users can freely input the artist's name and it would be created if it doens't exist. I am getting the following error when inputting data in the artist field: ValueError: Cannot assign ''testing'': 'Profile.artist' must...
bayman
2

votes
1

answer
501

views

How to put button on same row as django-bootstrap3 pagination

I am using django-tables2 and django-bootstrap3 to render a table w/ pagination. I'd like to add a button on the same row as the pagination but my Submit button is showing below the pagination. How should I make the button align to the bottom right of the table along the same row as the pagination...
bayman
1

votes
2

answer
386

views

Django Introduce a foreign key from a model CharField

I have migrated over 10,000 records from my old mySQL database to Django/sqlite. In my old mysql schema's Song table, the artist field was not a 1 to many field but was just a mysql varchar field. In my new Django model, I converted the artist field to a ForeignKey and used temp_artist to temporari...
bayman
1

votes
2

answer
697

views

How to add counter column in django-tables2?

I'm trying to add a counter on the first column of a table using django-tables2, but the solution below is only showing all 0 under the # column. How should I add a column that will have a column that numbers the rows? tables.py: import django_tables2 as tables from profiles.models import Track imp...
bayman
2

votes
1

answer
615

views

How to get last login ip in django and save to a GenericIPAddressField?

I'm extending the user profile and added a last_ip field as shown below. How do I update this field whenever a user is logged in to its current IP? I am using allauth if it matters. class UserProfile(models.Model): user = models.OneToOneField(User) last_ip = models.GenericIPAddressField(protocol=...
bayman
2

votes
2

answer
279

views

django saving list of foreignkey objects to m2m field w/ through model with ordering

I have a html table w/ javascript that allows ordering of rows which passes the track_id list and row order list through a form on POST. I just added the class PlaylistTrack using through for the model so I can add ordering to the tracks.m2m field. The view I have below works before I added the thr...
bayman
4

votes
3

answer
1.9k

views

How to retrieve foreign key field in Django rest framework?

Given the model and serializer classes below, when I retrieve Track details, it'll only show the Track title but not the related Artist. How would I also show the Artist name when retrieving Track details? models.py class Artist (models.Model): name = models.CharField(max_length=100) def __str__(se...
bayman
5

votes
1

answer
459

views

Django year validation returns “Ensure this value is less than or equal to 2016” in year 2017

In my database, I have a record where the year field is 2016 but I need to change it to 2017. When I use Django admin to change it 2017, I get 'Ensure this value is less than or equal to 2016.'. What is wrong w/ my model? class Track (models.Model): artist = models.ForeignKey(Artist, blank=True...
bayman
2

votes
1

answer
190

views

Django-tables2 KeyError when field has {curly brackets}

I have a model with Artist and Track class and when the title field of a Track has curly brackets, I get a KeyError Exception. (E.g. Track Title 'Beat It {Freestyle}'). Looks like django_tables2 is passing my Track title field value to string.format(). How do I prevent that in django_tables2? model...
bayman
1

votes
2

answer
483

views

Python3 how to get current and next directory from os.walk?

How do I traverse through a directory and print out the current directory and the next directory in the iteration? currentdir = '/tmp/test' for root, dirs, files in os.walk(currentdir): print('Current dir: {}: '.format(root)) print('Next dir: {}: '.format(next(os.walk(root))))
bayman
1

votes
1

answer
51

views

How do I check for a condition before writing an empty file in Python?

I need to read all rows in data.csv and if the value is Los Angeles for the City column, then it would add that row to match.csv but I don't want it to create match.csv with a header and empty rows if there are no rows with Los Angeles in the City column from data.csv. How do I do this? with open('...
bayman
2

votes
1

answer
356

views

Django serializer classes referencing each other

I need to have two serializer classes referencing each other but I'm getting one of the classes not defined if both classes are referencing each other. I created a duplicate of one of the class w/ a difference name and this seems to work but is there a better way to do this without making 3 classes...
bayman
0

votes
1

answer
281

views

Nginx, uwsgi, django, ubuntu 16 issue w/ static files

I am using Nginx, uwsgi, django and Ubuntu 16.04 and getting 404 on all my static files. When I look at nginx's error.log it's adding an extra /static in front of the actual file location: 404 Not found: /home/jsmith/firstproj/static_cdn/static/css/bootstrap.css Actual location: /home/jsmith/first...
bayman